What JSON Schema is
JSON Schema is a way of describing what a JSON document should look like. You write the rules as JSON, then check documents against them. It is useful for catching missing fields, wrong types and out-of-range values before they cause trouble elsewhere.
Here is a small schema:
{
"type": "object",
"required": ["name"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 }
},
"additionalProperties": false
}
It allows an object with a non-empty string name and, optionally, a whole-number age of 0 or more. Any other key is rejected.
How to use the tool
- Paste your schema into the JSON Schema editor. The tool opens with an example schema, and pasting replaces it.
- Paste the document you want to check into JSON to validate. The example document contains deliberate mistakes, and pasting replaces it too.
- Leave Schema version on its default, or choose a draft yourself.
- Read the result below the editors. It updates as you type.
Validation runs in your browser. Nothing is uploaded.
Reading the results
The result reads Valid or Not valid: N problems. When there are problems, a table lists every one of them, not just the first. It has three columns:
- Where is the JSONPath of the failing value, such as
$.roles[1]. - Problem is the message, such as
String is too short (0 < 1). - Rule is the schema keyword that failed, such as
minimum,type,enumorrequired.
If either editor contains a JSON syntax error, the tool shows it with a line and column. Fix those first, because a document that cannot be parsed cannot be checked against a schema.
The tool uses the open-source @cfworker/json-schema library. It interprets schemas rather than compiling them to JavaScript, so it works under a strict Content-Security-Policy. Format keywords such as email, date-time and uri are checked. A $ref works within the same schema, for example #/$defs/address. References to other files or URLs are not fetched.
Choosing a draft
By default the tool reads the $schema keyword. It treats draft-04 as Draft 4, draft-06 and draft-07 as Draft 7, and 2019-09 as Draft 2019-09. Anything else, including a missing $schema, becomes Draft 2020-12. You can override this with the Schema version select, which offers Draft 2020-12, Draft 2019-09, Draft 7 and Draft 4.
Two changes matter most in practice:
- Drafts 2019-09 and 2020-12 use
$defsfor reusable pieces. Earlier drafts usedefinitions. - Draft 2020-12 uses
prefixItemsfor tuple-style arrays. Earlier drafts used an array form ofitems.
Common mistakes
- Forgetting
additionalProperties: false. Without it, any extra keys are allowed. - Putting
requiredin the wrong place. It belongs at the object level, as an array of names, not inside an individual property. - Expecting
integerto reject1.0. Most validators reject1.5but accept1.0, because the value is mathematically a whole number.
Validating in code
Once a schema behaves as you want, you can use it in your own project. With Ajv in JavaScript:
import Ajv from 'ajv/dist/2020';
const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(schema);
validate(data) || console.log(validate.errors);
The allErrors option reports every problem rather than stopping at the first. Ajv does not check formats on its own, so add the ajv-formats package if you want email and similar keywords enforced.
In Python, use the jsonschema package:
from jsonschema import validate
validate(instance=data, schema=schema)
This raises a ValidationError if the data does not match the schema.