Blog post
JSON Schema Validation: Beginner to Advanced
Learn JSON Schema — write schemas, validate data, use $ref, combine schemas, and integrate validation in Node.js and Python.
Shashank Jain
Author


Article
What Is JSON Schema?
JSON Schema is a vocabulary for describing the structure and constraints of JSON data. It lets you define what fields are required, what types they must be, and what values are valid — then validate any JSON document against that schema.
Basic Schema Structure
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User",
"type": "object",
"required": ["id", "name", "email"],
"properties": {
"id": { "type": "integer", "minimum": 1 },
"name": { "type": "string", "minLength": 1, "maxLength": 100 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "maximum": 150 },
"roles": {
"type": "array",
"items": { "type": "string", "enum": ["admin", "user", "moderator"] },
"uniqueItems": true
}
},
"additionalProperties": false
}Validation in Node.js (AJV)
const Ajv = require('ajv');
const addFormats = require('ajv-formats');
const ajv = new Ajv();
addFormats(ajv);
const schema = {
type: 'object',
required: ['name', 'email'],
properties: {
name: { type: 'string' },
email: { type: 'string', format: 'email' }
}
};
const validate = ajv.compile(schema);
const valid = validate({ name: 'Alice', email: 'alice@example.com' });
if (!valid) console.log(validate.errors);Validation in Python (jsonschema)
from jsonschema import validate, ValidationError
schema = {
'type': 'object',
'required': ['name', 'email'],
'properties': {
'name': {'type': 'string'},
'email': {'type': 'string', 'format': 'email'}
}
}
try:
validate({'name': 'Alice', 'email': 'alice@example.com'}, schema)
print('Valid')
except ValidationError as e:
print(f'Invalid: {e.message}')Schema Composition
// $ref — reuse schema definitions
{
"$defs": {
"Address": {
"type": "object",
"required": ["street", "city"],
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
}
}
},
"type": "object",
"properties": {
"home": { "$ref": "#/$defs/Address" },
"work": { "$ref": "#/$defs/Address" }
}
}
// allOf, anyOf, oneOf
{
"oneOf": [
{ "$ref": "#/$defs/Cat" },
{ "$ref": "#/$defs/Dog" }
]
}FAQ
What JSON Schema draft should I use?
Use Draft 2020-12 for new projects. It is the latest stable version with the best tooling support. Avoid Draft 4 and Draft 6 unless you need backward compatibility.
What is the difference between required and minProperties?
required lists specific property names that must be present. minProperties sets a minimum count of properties regardless of which ones they are.
Can JSON Schema validate email addresses?
Yes, using format: 'email'. Note that format validation is optional in the spec — libraries like AJV require ajv-formats to enable it.
How do I validate JSON Schema online?
Use the JSON Schema Validator on jsondecode.com.
Is JSON Schema the same as TypeScript interfaces?
No. TypeScript interfaces are compile-time only. JSON Schema validates at runtime against actual data. You can generate TypeScript types from JSON Schema using tools like json-schema-to-typescript.
Keep reading
Recent blogs

Jun 14, 2026
JSON in C#: System.Text.Json and Newtonsoft Complete Guide
Serialize and deserialize JSON in C# using System.Text.Json and Newtonsoft.Json with practical examples.

Jun 14, 2026
JSON to Markdown Table: Convert JSON Arrays Instantly
Convert JSON arrays to Markdown tables in JavaScript, Python, and with the free online tool.

Jun 14, 2026
JSON in TypeScript: Type-Safe Parsing and Validation
Stop using any for JSON in TypeScript — use Zod, type guards, and generics for fully type-safe parsing.