JSON Schema Explained, with Examples
· 3 min read
JSON Schema is a way to describe what a JSON document should contain. You write the description once, then check any document against it. A schema can say which fields must be present, what type each value has and which values are allowed.
This guide explains the main keywords with one worked example. Paste the examples into the JSON Schema Validator to see the results as you read.
Syntax checking and schema validation
These are two different checks. Syntax checking confirms that text is well-formed JSON, with matching brackets and quoted keys. Schema validation starts after that: it checks that well-formed JSON has the right content, such as required fields, types, ranges and formats.
If you only need a syntax check, the JSON Validator is simpler.
A schema is JSON
A schema is itself a JSON document, so there is no new file format to learn, only a set of keywords. These are the ones you will use most:
typesets the kind of value: string, number, integer, boolean, object, array or null.requiredlists the property names that must be present.propertiesdescribes each named property of an object.minLengthsets the shortest allowed string, andminimumthe lowest allowed number.formatchecks that a string has a known shape, such as email, date-time or uri.additionalProperties: falserejects any property not listed inproperties.enumlimits a value to a fixed list.itemsdescribes the elements of an array.$defsand$reflet you reuse parts of a schema.
A worked example
This schema describes a person with a name, an email address and an optional age:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0 }
},
"additionalProperties": false
}
The document must be an object with name and email. The name must be a string of at least one character, and the email must look like an email address. The age is not in required, so it may be left out, but if it is there it must be a whole number of 0 or more. Because additionalProperties is false, no other properties are allowed.
This document passes every rule:
{ "name": "Ada", "email": "ada@example.com", "age": 36 }
Reading the results for an invalid document
This document breaks several rules at once:
{ "name": "", "email": "not-an-email", "age": -3, "admin": true }
The validator lists every problem, not just the first, each with its location, the keyword that failed and a message:
| Location | Keyword | Message |
|---|---|---|
$.name | minLength | String is too short (0 < 1). |
$.email | format | String does not match format "email". |
$.age | minimum | -3 is less than 0. |
$ | additionalProperties | Property "admin" does not match additional properties schema. |
$.admin | false | This property isn’t allowed by the schema. |
The first three rows are separate problems: the name is empty, the email is not an email address and the age is below zero.
The last two rows are the same problem reported twice. admin is not listed in properties, so it is reported once at the object ($) and once at the property itself ($.admin). Removing admin clears both.
Reusing parts with $defs and $ref
Large schemas often repeat a shape, such as an address. Define it once under $defs, then point to it with $ref and a path such as #/$defs/address.
A $ref only works within the same schema: references to other files or URLs are not fetched. If you need a piece of another schema, copy it into $defs.
Choosing a draft
JSON Schema has several versions, called drafts, and the $schema keyword says which one a schema uses. The example above uses Draft 2020-12.
The validator supports Draft 2020-12, 2019-09, Draft 7 and Draft 4. It reads $schema to choose, falls back to 2020-12 when it is missing, and lets you pick a draft yourself.
Where schemas are useful
- Checking API requests and responses.
- Checking config files and test fixtures.
- Documenting a format, since the schema states exactly what is allowed.
The validator runs in your browser, so your schema and document are not uploaded.
Try it
Paste the example schema and the invalid document into the JSON Schema Validator, then fix the document one problem at a time.