JSON Schema Tutorial: Validate JSON Like a Pro (2026 Guide)
What is JSON Schema?
JSON Schema is a vocabulary for describing the structure and constraints of JSON data. While JSON tells you *what* the data is, JSON Schema tells you *what it should look like* — required fields, allowed types, value ranges, string patterns, and more.
Think of it as a contract between systems: the schema is the rulebook, your JSON is the document being checked against it.
A minimal example
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"email":{ "type": "string", "format": "email" }
},
"required": ["name", "email"]
}
This schema requires every object to have a non-empty name and a valid email. age is optional but must be a non-negative integer when present.
Core keywords you'll use every day
- type —
string,number,integer,boolean,array,object,null - required — array of mandatory property names
- properties — defines each field's sub-schema
- additionalProperties — set to
falseto reject unknown keys - enum — restrict a value to a fixed list
- pattern — regex constraint on strings
- minLength / maxLength / minimum / maximum
- items — schema for elements of an array
- \$ref — reference another schema, allowing reuse
Validating in Node.js with Ajv
Ajv is the de-facto JSON Schema validator for JavaScript:
npm install ajv ajv-formats
import Ajv from 'ajv'
import addFormats from 'ajv-formats'
const ajv = new Ajv({ allErrors: true })
addFormats(ajv)
const validate = ajv.compile(schema)
const ok = validate(data)
if (!ok) console.error(validate.errors)
Validating in Python with jsonschema
pip install jsonschema
from jsonschema import validate, ValidationError
try:
validate(instance=data, schema=schema)
except ValidationError as e:
print(e.message)
Best practices
- Start strict, loosen when needed. Begin with
additionalProperties: falseand required fields, then relax as real-world data forces you to. - Use \$ref for reusable types. Define
Address,Money,Timestamponce and reference them everywhere. - Version your schemas the same way you version APIs.
- Validate at the boundary. Once data is inside your trusted code, repeated validation is wasted CPU.
- Generate types from schemas. Tools like
json-schema-to-typescriptkeep TS types in sync automatically.
When NOT to use JSON Schema
For internal data that never crosses a process boundary, type systems like TypeScript or Pydantic are usually faster, more ergonomic and catch more issues at compile time.
Try it now
Paste your JSON into the JSONNeat formatter to make sure it's syntactically valid before you try to validate against a schema.
Try the tools: JSON Formatter · Validator · Minifier