JSON Schema Validation: A Practical Guide
JSON Schema is a declarative language for validating the structure, content, and format of JSON data. If you build APIs or process JSON from external sources, schema validation prevents malformed data from causing bugs downstream.
Why Validate JSON?
JSON.parse() only checks syntax — it confirms that the string is valid JSON. But it does not verify that the data contains the fields you expect, with the types you need:
// Valid JSON, but is it valid for YOUR application?
const data = JSON.parse('{"name": 123, "email": null}');
// name should be a string, email should be a valid address
JSON Schema solves this by letting you define expectations:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" }
}
}
Core Keywords
Type Checking
{ "type": "string" }
{ "type": "number", "minimum": 0, "maximum": 100 }
{ "type": "array", "items": { "type": "string" }, "minItems": 1 }
{ "type": "object" }
Required Fields
{
"type": "object",
"required": ["id", "name"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"bio": { "type": "string" }
}
}
Here id and name are mandatory; bio is optional.
Nested Objects
{
"type": "object",
"properties": {
"address": {
"type": "object",
"required": ["city", "country"],
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"country": { "type": "string", "minLength": 2, "maxLength": 2 }
}
}
}
}
Enums and Patterns
{
"status": { "type": "string", "enum": ["active", "inactive", "banned"] },
"zipCode": { "type": "string", "pattern": "^[0-9]{5}$" }
}
Test your regex patterns used in JSON Schema validation with our Regex Tester before deploying.
Integrating JSON Schema in Your Workflow
Node.js — Ajv
import Ajv from 'ajv';
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(data);
if (!valid) console.log(validate.errors);
Python — jsonschema
from jsonschema import validate, ValidationError
try:
validate(instance=data, schema=schema)
except ValidationError as e:
print(e.message)
From JSON Schema to TypeScript
Once you define a JSON Schema, you can generate TypeScript interfaces automatically. This creates a single source of truth for both runtime validation and compile-time type checking.
Try it: Convert your JSON data to TypeScript interfaces instantly with our JSON to TypeScript Converter.
Best Practices
- Start with
required— Explicitly list required fields; don’t rely on defaults. - Use
additionalProperties: false— Reject unexpected fields for strict validation. - Version your schemas — Include a
$idwith version number for API evolution. - Validate early — Run schema validation at the API boundary, not deep in business logic.
- Validate your JSON first — Before testing schemas, ensure your JSON is syntactically valid with our JSON Formatter.
This article is part of our JSON Developer Guide series.