JSON Schema in One Sentence
A JSON Schema is itself a JSON document that sets the rules for other JSON. Think of it as a form with required fields: the data either fits the rules and is valid, or it breaks a rule and you get a clear error that says where and why.
A Complete Example
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User",
"type": "object",
"properties": {
"id": { "type": "integer", "minimum": 1 },
"name": { "type": "string", "minLength": 2 },
"email": { "type": "string", "format": "email" },
"role": { "enum": ["admin", "editor", "viewer"] },
"tags": { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
},
"required": ["id", "name", "email"],
"additionalProperties": false
}This data passes every rule:
{ "id": 1, "name": "Emily Clark", "email": "emily@example.com", "role": "admin", "tags": ["json"] }This data breaks five rules:
{ "id": 0, "name": "E", "email": "not-an-email", "role": "owner", "nickname": "Em" }(root) has an extra field "nickname" that is not allowed /id must be >= 1 /name must NOT have fewer than 2 characters /email must match format "email" /role must be one of: "admin", "editor", "viewer"
Paste both into the JSON Schema Validator to see these errors yourself.
The Keywords You Will Use Most
| Keyword | What it checks |
|---|---|
type | string, number, integer, boolean, object, array or null |
properties | The rules for each field of an object |
required | Fields that must be present |
additionalProperties | Whether fields not listed are allowed |
minimum / maximum | Limits for numbers |
minLength / maxLength | Limits for text length |
pattern | A regular expression the text must match |
format | email, date, date-time, uri, uuid, ipv4 and more |
enum | A fixed list of allowed values |
items / minItems / uniqueItems | Rules for arrays |
Optional and Nullable Fields
A field that is not in required may be left out. A field that may be present but empty should allow null as a type:
"manager": { "type": ["string", "null"] }Reusing Rules with $defs and $ref
When the same shape appears in several places, define it once and point to it:
{
"$defs": {
"address": {
"type": "object",
"properties": { "city": { "type": "string" }, "zip": { "type": "string" } },
"required": ["city"]
}
},
"type": "object",
"properties": {
"home": { "$ref": "#/$defs/address" },
"work": { "$ref": "#/$defs/address" }
}
}Which Version Should I Use?
The $schema line says which version of JSON Schema you follow. Use draft 2020-12 for new work; it is the current version and is used by OpenAPI 3.1. Many existing projects still use draft-07, which is also widely supported. Older schemas use definitions where newer ones use $defs.
Where JSON Schema Is Used
- APIs: check request bodies before your code runs, and describe them in OpenAPI docs.
- Config files: editors like VS Code use schemas to autocomplete and check files such as package.json and tsconfig.json.
- Forms: libraries can build forms and their validation from a schema.
- Data pipelines: reject bad records before they reach a database.
Validate JSON in Code
import Ajv from 'ajv/dist/2020'
import addFormats from 'ajv-formats'
const ajv = new Ajv({ allErrors: true })
addFormats(ajv)
const validate = ajv.compile(schema)
if (!validate(data)) console.log(validate.errors)from jsonschema import Draft202012Validator
validator = Draft202012Validator(schema)
for error in validator.iter_errors(data):
print(error.json_path, error.message)Get Started in a Minute
You do not have to write a schema by hand. Paste a sample of your data into the JSON Schema Generator, add the extra rules you need, then test real data with the JSON Schema Validator.
Frequently Asked Questions
What is JSON Schema used for?
JSON Schema describes the structure of valid JSON: required fields, value types and allowed values. It is used to validate API requests, document APIs in OpenAPI, check config files in editors and build forms.
Is JSON Schema the same as JSON?
A JSON Schema is written in JSON, but it is a set of rules for other JSON documents. The data is the JSON; the schema describes what that data must look like.
Which JSON Schema version should I use?
Use draft 2020-12 for new projects. Draft-07 is older but still widely supported. The $schema keyword at the top says which version a schema uses.
How do I create a JSON Schema quickly?
Paste a sample of your JSON into our free JSON Schema Generator, then add extra rules like minLength, format or enum, and test it with the JSON Schema Validator.