JSON Schema Validation: A Complete Guide
JSON Schema is the standard way to describe and validate the structure of JSON data. If you have ever worked with an API that returns different shapes of data depending on the endpoint, or needed to enforce that a config file always has a required port field of type number, JSON Schema is the tool for the job.
This guide covers everything you need to know: what JSON Schema is, how the different draft versions compare, the most important keywords, how to validate in JavaScript and Python, and how to use IRJSON's free online validator.
What is JSON Schema?
JSON Schema is a JSON document that describes the valid structure of another JSON document. It specifies which fields are required, what type each field must be, minimum and maximum values for numbers, length constraints for strings, allowed values with enums, and complex structural rules using composition keywords.
A simple schema that validates a user profile object:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["id", "name", "email"],
"properties": {
"id": { "type": "integer", "minimum": 1 },
"name": { "type": "string", "minLength": 1, "maxLength": 100 },
"email": { "type": "string", "format": "email" },
"role": { "type": "string", "enum": ["admin", "editor", "viewer"] },
"createdAt": { "type": "string", "format": "date-time" }
},
"additionalProperties": false
}
JSON Schema Draft Versions
JSON Schema has evolved through several published drafts. Understanding which draft you need matters because validators, IDE plugins and code generators have varying levels of support.
| Draft | Year | Key Additions | Compatibility |
|---|---|---|---|
| Draft-04 | 2013 | Foundation: type, properties, required | Widest — old tooling |
| Draft-06 | 2017 | exclusiveMinimum/Maximum as numbers, contains | Good |
| Draft-07 | 2019 | if/then/else, readOnly/writeOnly, contentEncoding | Excellent — most common |
| 2019-09 | 2019 | $recursiveRef, unevaluatedProperties, $anchor | Good |
| 2020-12 | 2020 | prefixItems, $dynamicRef, improved unevaluatedItems | Good — use for new projects |
Recommendation: Use Draft 2020-12 for new projects. Use Draft-07 if you need maximum compatibility with older tooling, IDE JSON Schema support, and code generators.
Core JSON Schema Keywords
Type Validation
{ "type": "string" } // string
{ "type": "number" } // any number (integer or float)
{ "type": "integer" } // whole numbers only
{ "type": "boolean" } // true or false
{ "type": "array" } // JSON array
{ "type": "object" } // JSON object
{ "type": "null" } // JSON null
{ "type": ["string", "null"] } // nullable string
String Constraints
{
"type": "string",
"minLength": 3,
"maxLength": 50,
"pattern": "^[a-zA-Z0-9_]+$"
}
Number Constraints
{
"type": "number",
"minimum": 0,
"maximum": 100,
"multipleOf": 0.01
}
Object Constraints
{
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string" },
"email": { "type": "string", "format": "email" }
},
"minProperties": 1,
"additionalProperties": false
}
Array Constraints
{
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}
Composition Keywords
// Must match ALL schemas
{ "allOf": [{ "type": "string" }, { "minLength": 5 }] }
// Must match AT LEAST ONE schema
{ "anyOf": [{ "type": "string" }, { "type": "number" }] }
// Must match EXACTLY ONE schema
{ "oneOf": [{ "type": "integer" }, { "type": "boolean" }] }
// Conditional validation (Draft-07+)
{
"if": { "properties": { "type": { "const": "admin" } } },
"then": { "required": ["adminKey"] },
"else": { "properties": { "adminKey": false } }
}
Validating JSON with AJV in JavaScript
AJV (Another JSON Validator) is the standard choice for JSON Schema validation in JavaScript. It compiles schemas to optimised JavaScript functions for maximum performance.
import Ajv from 'ajv'
import addFormats from 'ajv-formats'
const ajv = new Ajv({ allErrors: true })
addFormats(ajv) // adds format validators: email, date, uri, etc.
const schema = {
type: 'object',
required: ['name', 'email'],
properties: {
name: { type: 'string', minLength: 1 },
email: { type: 'string', format: 'email' }
}
}
const validate = ajv.compile(schema)
const data = { name: 'Alice', email: 'alice@example.com' }
const valid = validate(data)
if (!valid) {
console.log(validate.errors)
// [{ instancePath: '/email', message: 'must match format "email"', ... }]
}
Validating JSON in Python
import jsonschema
from jsonschema import validate, ValidationError
schema = {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {"type": "string", "minLength": 1},
"email": {"type": "string", "format": "email"}
}
}
data = {"name": "Alice", "email": "alice@example.com"}
try:
validate(instance=data, schema=schema)
print("Valid!")
except ValidationError as e:
print(f"Validation error: {e.message}")
print(f"Path: {list(e.absolute_path)}")
Try the JSON Schema Validator Online
IRJSON includes a free browser-based JSON Schema validator powered by AJV. Paste your schema and JSON document and see validation results in real time:
Generating JSON Schema from Existing Data
If you have existing JSON and want to generate a schema from it, IRJSON can infer a schema automatically. Open your JSON in the viewer and click the Schema tab — IRJSON will generate a draft JSON Schema based on the types and structure it detects. The generated schema is a starting point that you refine by adding constraints.
Common JSON Schema Mistakes
- Forgetting
required— properties defined underpropertiesare optional by default. You must list them inrequiredto enforce their presence. - Using
additionalProperties: falsetoo aggressively — this blocks all undeclared properties, which breaks forward compatibility. Consider omitting it in API response schemas. - Not using
ajv-formats— AJV does not validateformatkeywords (email, uri, date) by default. You must install and register theajv-formatsplugin. - Draft mismatch — mixing Draft-07 features in a 2020-12 schema (or vice versa) can cause subtle validator differences. Always specify
$schemaexplicitly.
Validate JSON Schema online for free
IRJSON's Schema Validator uses AJV with all major drafts supported. Paste your schema and JSON to get instant, detailed validation feedback.
Open Schema Validator →