jsonschema
is an implementation of JSON Schema for Python (supporting 2.7+ including Python 3).
In [ ]:
from jsonschema import validate
In [ ]:
# A sample schema, like what we'd get from json.load()
schema = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
},
}
In [ ]:
# If no exception is raised by validate(), the instance is valid.
validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)
In [ ]:
validate(
instance={"name" : "Eggs", "price" : "Invalid"},
schema=schema,
)
It can also be used from console:
In [ ]:
!echo '{"name" : "Eggs", "price" : 34.99}' > /tmp/sample.json
In [ ]:
!echo '{"type" : "object", "properties" : {"price" : {"type" : "number"}, "name" : {"type" : "string"}}}' > /tmp/sample.schema
In [ ]:
!jsonschema -i /tmp/sample.json /tmp/sample.schema
Try jsonschema
youself by adding your code below and running your own experiments 👇
In [ ]:
import jsonschema
# your code here
jsonschema.