What is JSON Schema?
JSON Schema is a declarative language for validating the structure of JSON data. It’s like a contract that defines what your JSON should look like.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User",
"description": "A user in the system",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
},
"email": {
"type": "string",
"format": "email"
}
},
"required": ["name", "age"],
"additionalProperties": false
}
Basic Types
Strings
{
"name": { "type": "string", "pattern": "^[A-Za-z]+$" },
"email": { "type": "string", "format": "email" }
}
Formats: email, uri, date, date-time, time, ipv4, ipv6, uuid
Numbers
{
"type": "number",
"minimum": 0,
"exclusiveMaximum": 100,
"multipleOf": 0.01
}
Arrays
{
"type": "array",
"items": { "type": "string" },
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}
Objects
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
},
"required": ["name"],
"additionalProperties": false,
"minProperties": 1,
"maxProperties": 10
}
Composition
allOf (AND)
{
"allOf": [
{ "type": "object", "properties": { "name": { "type": "string" } } },
{ "type": "object", "properties": { "age": { "type": "integer" } } }
]
}
anyOf (OR)
{
"anyOf": [
{ "type": "string" },
{ "type": "integer" }
]
}
oneOf (XOR)
{
"oneOf": [
{ "required": ["email"] },
{ "required": ["phone"] }
]
}
$ref (Reuse)
{
"$defs": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
}
}
},
"type": "object",
"properties": {
"home": { "$ref": "#/$defs/address" },
"work": { "$ref": "#/$defs/address" }
}
}
Conditional Validation
{
"type": "object",
"properties": {
"country": { "type": "string" },
"postalCode": { "type": "string" }
},
"if": {
"properties": { "country": { "const": "US" } },
"required": ["country"]
},
"then": {
"properties": {
"postalCode": { "pattern": "^[0-9]{5}(-[0-9]{4})?$" }
}
},
"else": {
"properties": {
"postalCode": { "minLength": 3 }
}
}
}
Real-World Examples
API Request Validation
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"username": {
"type": "string",
"minLength": 3,
"maxLength": 20,
"pattern": "^[a-zA-Z0-9_]+$"
},
"password": {
"type": "string",
"minLength": 8,
"pattern": "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).*$"
},
"email": {
"type": "string",
"format": "email"
},
"roles": {
"type": "array",
"items": {
"type": "string",
"enum": ["admin", "user", "moderator"]
},
"uniqueItems": true
}
},
"required": ["username", "password", "email"],
"additionalProperties": false
}
Configuration File
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"database": {
"type": "object",
"properties": {
"host": { "type": "string" },
"port": { "type": "integer", "default": 5432 },
"name": { "type": "string" }
},
"required": ["host", "name"]
},
"logging": {
"type": "object",
"properties": {
"level": {
"type": "string",
"enum": ["debug", "info", "warn", "error"]
},
"file": { "type": "string" }
}
}
},
"required": ["database"]
}
Tooling
Validation Libraries
| Language | Library |
|---|---|
| JavaScript | ajv, joi, zod |
| Python | jsonschema, pydantic |
| Java | everit-json-schema |
| Go | gojsonschema |
| C# | JsonSchema.Net |
Code Generation
# TypeScript from JSON Schema
npx json-schema-to-typescript schema.json > types.ts
# Python from JSON Schema
datamodel-codegen --input schema.json --output models.py
Documentation
# Generate HTML documentation (JavaScript)
npx @adobe/jsonschema2md -d schema.json -o docs
# Generate HTML documentation (Python)
pip install json-schema-for-humans && generate-schema-doc schema.json docs.html
Best Practices
- Start simple — Add constraints as needed
- Use $ref — Don’t repeat yourself
- Version your schemas — Include
$schemaand version in$id - Test thoroughly — Use both valid and invalid test cases
- Document formats — Custom formats need custom validators
- Consider performance — Complex schemas can be slow
Related Resources
- JSON Best Practices — Naming conventions, data types, and API design
- JSON Security — Protect against injection and validation bypass
- TypeScript JSON Guide — Runtime validation with Zod
- JSON Schema Validator — Validate your JSON against schemas
- JSON Formatter — Format your JSON for readability