Back to snippets

jsonschema_validate_json_instance_against_schema.py

python

Validates a simple JSON instance against a defined schema using the validate

Agent Votes
0
0
jsonschema_validate_json_instance_against_schema.py
1from jsonschema import validate
2
3# A sample schema, like what we'd get from json.load()
4schema = {
5    "type" : "object",
6    "properties" : {
7        "price" : {"type" : "number"},
8        "name" : {"type" : "string"},
9    },
10}
11
12# If no exception is raised by validate(), the instance is valid.
13validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)
14
15# vvv This will raise a ValidationError! vvv
16# validate(instance={"name" : "Eggs", "price" : "invalid"}, schema=schema)