Partial support for and , except for Show that can iteratively report all validation errors. Programmatic querying of which properties or items failed validation. Installation
$ pip install jsonschema ExtrasTwo extras are available when installing the package, both currently related to $ jsonschema --instance sample.json sample.schema0 validation:
They can be used when installing in order to include additional dependencies, e.g.: $ pip install jsonschema'[format]'
Be aware that the mere presence of these dependencies – or even the specification of $ jsonschema --instance sample.json sample.schema0 checks in a schema – do not activate format checks (as per the specification). Please read the for further details. Running the Test SuiteIf you have $ jsonschema --instance sample.json sample.schema4 installed (perhaps via $ jsonschema --instance sample.json sample.schema5 or your package manager), running $ jsonschema --instance sample.json sample.schema4 in the directory of your source checkout will run jsonschema ’s test suite on all of the versions of Python jsonschema supports. If you don’t have all of the versions that jsonschema is tested under, you’ll likely want to run using $ jsonschema --instance sample.json sample.schema4’s $ pip install jsonschema1 option. Of course you’re also free to just run the tests on a single version with your favorite test runner. The tests live in the $ pip install jsonschema2 package. Benchmarks
$ tox -e perf CommunityThe JSON Schema specification has a Slack, with an invite link on its home page. Many folks knowledgeable on authoring schemas can be found there. Otherwise, asking questions on Stack Overflow is another means of getting help if you’re stuck. AboutI’m Julian Berman.
Get in touch, via GitHub or otherwise, if you’ve got something to contribute, it’d be most welcome! You can also generally find me on Libera (nick: $ pip install jsonschema5) in various channels, including $ pip install jsonschema6. If you feel overwhelmingly grateful, you can also sponsor me. And for companies who appreciate The following example is by no means definitive of all the value JSON Schema can provide. For this you will need to go deep into the specification itself – learn more at https://json-schema.org/specification.html. Let’s pretend we’re interacting with a JSON based product catalog. This catalog has a product which has:
For example:
While generally straightforward, the example leaves some open questions. Here are just a few of them:
When you’re talking about a data format, you want to have metadata about what keys mean, including the valid inputs for those keys. JSON Schema is a proposed IETF standard how to answer those questions for data. Starting the schemaTo start a schema definition, let’s begin with a basic JSON schema. We start with four properties called keywords which are expressed as JSON keys.
We introduce the following pieces of terminology when we start the schema:
Defining the properties 0 is a numeric value that uniquely identifies a product. Since this is the canonical identifier for a product, it doesn’t make sense to have a product without one, so it is required.In JSON Schema terms, we update our schema to add:
Going deeper with propertiesAccording to the store owner there are no free products. ;)
Next, we come to the 3 key.The store owner has said this:
Therefore:
Nesting data structuresUp until this point we’ve been dealing with a very flat schema – only one level. This section demonstrates nested data structures.
References outside the schemaSo far our JSON schema has been wholly self contained. It is very common to share JSON schema across many data structures for reuse, readability and maintainability among other reasons. For this example we introduce a new JSON Schema resource and for both properties therein:
Next we add a reference to this new schema so it can be incorporated.
Taking a look at data for our defined JSON SchemaWe’ve certainly expanded on the concept of a product since our earliest sample data (scroll up to the top). Let’s take a look at data which matches the JSON Schema we have defined. |