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.
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
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"
}
}
}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
{
"$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
}
}
}
}
}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}")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.
Validate Your JSON with These Free Tools
Check your JSON against schemas, format, and validate it with our free online tools. No signup, everything runs in your browser.
JSON Formatter
Format and beautify JSON with proper indentation.
JSON Validator
Validate JSON syntax with detailed error messages.
JSON Minifier
Compress JSON by removing whitespace to reduce size.
JSON Beautifier
Turn minified JSON into clean, readable code.
CSV to JSON
Convert CSV data into JSON for APIs and apps.