JSON Schema Validator – A Complete Guide
JSON Schema lets you define the exact structure a JSON document must have — and then validate any JSON against it. This guide covers what JSON Schema is, how to write schemas, and how to validate in JavaScript and Python.
Before running JSON Schema validation, make sure your JSON is syntactically valid. Use our free JSON validator to catch syntax errors like missing commas or unquoted keys instantly.
What Is JSON Schema?
JSON Schema is a vocabulary for annotating and validating the structure of JSON. It is itself written in JSON and describes what keys are required, what types values must be, what patterns strings must match, and more.
Think of a JSON Schema as a contract: any JSON that passes validation is guaranteed to have the shape your code expects.
// A simple JSON Schema for a user object
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "age", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0, "maximum": 150 },
"email": { "type": "string", "format": "email" }
},
"additionalProperties": false
}
// This JSON PASSES the schema above:
{ "name": "Alice", "age": 30, "email": "[email protected]" }
// This JSON FAILS (age is a string, extra key "role" not allowed):
{ "name": "Bob", "age": "thirty", "email": "[email protected]", "role": "admin" }JSON Schema Versions (Drafts)
JSON Schema has evolved through several drafts. The most important ones to know:
http://json-schema.org/draft-07/schemahttps://json-schema.org/draft/2019-09/schemahttps://json-schema.org/draft/2020-12/schemaEssential JSON Schema Keywords
typeThe value type: "string", "number", "integer", "boolean", "array", "object", or "null"requiredArray of property names that must be present in an objectpropertiesDefines the schema for each named property of an objectadditionalPropertiesIf false, rejects any property not listed in propertiesminimum / maximumInclusive lower and upper bound for numeric valuesminLength / maxLengthMinimum and maximum length constraints for stringspatternA regular expression that string values must matchenumAn array of allowed values — the JSON value must equal one of themitemsSchema that every item in an array must matchminItems / maxItemsMinimum and maximum number of items in an array$refA reference to another schema definition (enables reusable sub-schemas)oneOf / anyOf / allOfLogical combinators: exactly one of, at least one of, or all of the given schemas must matchValidate with Ajv (JavaScript)
Ajv is the most popular JSON Schema validator for JavaScript. Install it: npm install ajv
import Ajv from "ajv";
const ajv = new Ajv();
const schema = {
type: "object",
required: ["name", "age"],
properties: {
name: { type: "string" },
age: { type: "integer", minimum: 0 },
},
additionalProperties: false,
};
const validate = ajv.compile(schema);
const data = { name: "Alice", age: 30 };
const valid = validate(data);
if (!valid) {
console.log(validate.errors);
// [{ instancePath: '/age', message: 'must be integer' }]
} else {
console.log("Valid!");
}Validate with jsonschema (Python)
Install: pip install jsonschema
import json
import jsonschema
schema = {
"type": "object",
"required": ["name", "age"],
"properties": {
"name": {"type": "string"},
"age": {"type": "integer", "minimum": 0},
},
"additionalProperties": False,
}
def validate_user(raw_json: str):
data = json.loads(raw_json) # Step 1: syntax check
jsonschema.validate(data, schema) # Step 2: schema check
try:
validate_user('{"name": "Alice", "age": 30}')
print("Valid!")
except json.JSONDecodeError as e:
print(f"Syntax error: {e.msg}")
except jsonschema.ValidationError as e:
print(f"Schema error: {e.message}")Frequently Asked Questions
What is the difference between JSON validation and JSON Schema validation?+
JSON validation checks that a string is syntactically valid JSON (correct brackets, double quotes, no trailing commas, etc.). JSON Schema validation goes further — it checks that the JSON has the correct structure, data types, and constraints defined in a schema file. You need both: syntax first, then schema.
What JSON Schema draft should I use?+
Use JSON Schema 2020-12 (the latest) for new projects, unless your tooling only supports an older draft. Draft 7 is still widely supported and is a good fallback. Drafts 2019-09 and 2020-12 are largely compatible, with 2020-12 adding features like prefixItems for tuple validation.
What is the best JSON Schema validator for JavaScript?+
Ajv (Another JSON Validator) is the most widely used JSON Schema validator for JavaScript and Node.js. It supports Draft 7, 2019-09, and 2020-12, is extremely fast, and is used by millions of packages. Install it with: npm install ajv
Can I validate JSON Schema online?+
For basic JSON syntax validation (checking that your JSON file is syntactically valid), use json-validator.net. For full JSON Schema validation (checking that JSON matches a schema), you need a dedicated tool that accepts both a JSON instance and a schema file.
What is the $schema keyword in JSON Schema?+
The $schema keyword declares which version of the JSON Schema specification your schema uses. For example: "$schema": "https://json-schema.org/draft/2020-12/schema". It helps validators know which rules to apply. It is optional but strongly recommended.
Check JSON Syntax Before Schema Validation
A JSON Schema validator can't run on syntactically broken JSON. Use this free tool first.
Validate JSON Syntax