Jsonschema¶
Description:
- jsonschema is an implementation of the JSON Schema specification for Python.
Resources:
- [GitHub] https://github.com/python-jsonschema/jsonschema
- [GitHub Jsonschema specifications] https://github.com/python-jsonschema/jsonschema-specifications
Source and license:
- Some of these examples are adapted from the jsonschema README (https://github.com/python-jsonschema/jsonschema), Copyright (c) 2013 Julian Berman, licensed under MIT. That code stays under its own license; see THIRD_PARTY_NOTICES.md.
Example 1¶
Validate a schema
In [ ]:
Copied!
from jsonschema import validate
# A sample schema, like what we'd get from json.load()
schema = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
},
}
# If no exception is raised by validate(), the instance is valid.
# This is valid
validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)
# This is invalid
validate(
instance={"name" : "Eggs", "price" : "Invalid"}, schema=schema,
)
from jsonschema import validate
# A sample schema, like what we'd get from json.load()
schema = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
},
}
# If no exception is raised by validate(), the instance is valid.
# This is valid
validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)
# This is invalid
validate(
instance={"name" : "Eggs", "price" : "Invalid"}, schema=schema,
)