Advanced Guide
JSON Schema Validation: Complete Developer Guide
Learn how to use JSON Schema for data validation. Includes practical examples, best practices, and implementation tips for building robust APIs and applications.
What is JSON Schema?
JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. It provides a contract for your JSON data, ensuring it meets specific requirements.
Key Benefits:
- ✅ Validates data structure and types
- ✅ Documents your data format
- ✅ Generates documentation automatically
- ✅ Enables code generation
- ✅ Improves API reliability
Basic Schema Example
JSON Data:
{
"name": "John Doe",
"age": 30,
"email": "john@example.com"
}JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/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"
}
}
}Common Schema Keywords
Type Keywords
type- Data typeenum- Allowed valuesconst- Constant value
String Keywords
minLength- Min lengthmaxLength- Max lengthpattern- Regex patternformat- Format (email, uri)
Number Keywords
minimum- Min valuemaximum- Max valuemultipleOf- Multiple of
Array Keywords
items- Item schemaminItems- Min itemsmaxItems- Max itemsuniqueItems- Unique items
Advanced Schema Example
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "User",
"description": "A user in the system",
"type": "object",
"required": ["id", "username", "email"],
"properties": {
"id": {
"type": "integer",
"description": "Unique user identifier"
},
"username": {
"type": "string",
"minLength": 3,
"maxLength": 20,
"pattern": "^[a-zA-Z0-9_]+$"
},
"email": {
"type": "string",
"format": "email"
},
"roles": {
"type": "array",
"items": {
"type": "string",
"enum": ["admin", "user", "moderator"]
},
"minItems": 1,
"uniqueItems": true
},
"profile": {
"type": "object",
"properties": {
"firstName": { "type": "string" },
"lastName": { "type": "string" },
"age": {
"type": "integer",
"minimum": 13,
"maximum": 120
}
}
}
}
}Implementation Examples
JavaScript (Ajv)
const Ajv = require('ajv');
const ajv = new 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(data);
if (!valid) {
console.log(validate.errors);
}Python (jsonschema)
from jsonschema import validate, ValidationError
schema = {
"type": "object",
"required": ["name", "email"],
"properties": {
"name": {"type": "string"},
"email": {"type": "string", "format": "email"}
}
}
try:
validate(instance=data, schema=schema)
print("Valid!")
except ValidationError as e:
print(f"Invalid: {e.message}")Best Practices
Use Descriptive Titles and Descriptions
Help developers understand your schema with clear documentation
Define Required Fields
Always specify which fields are required vs optional
Use Appropriate Constraints
Add min/max, patterns, and formats to validate data properly
Version Your Schemas
Track schema changes and maintain backward compatibility
Validate Your JSON Data
Use our free JSON validator to test your data against schemas and ensure data integrity.