JSON Schema is a vocabulary for validating the structure and content of JSON data. Define what fields are required, what types they must be, and what values are allowed — then validate any JSON against that contract.
A JSON Schema is itself a JSON document that describes the expected shape of another JSON document. You write a schema once, then use a validator library (like AJV in JavaScript, or jsonschema in Python) to validate incoming data against it at runtime.
JSON Schema is the foundation of OpenAPI/Swagger specs, Pydantic models, TypeScript type generation, and form validation in countless applications.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User",
"type": "object",
"required": ["id", "name", "email"],
"properties": {
"id": { "type": "integer", "minimum": 1 },
"name": { "type": "string", "minLength": 2, "maxLength": 100 },
"email": { "type": "string", "format": "email" },
"age": { "type": "integer", "minimum": 0, "maximum": 150 },
"role": { "type": "string", "enum": ["admin", "user", "guest"] }
},
"additionalProperties": false
}{
"type": "array",
"items": {
"type": "object",
"required": ["id", "price"],
"properties": {
"id": { "type": "string" },
"price": { "type": "number", "minimum": 0 },
"tags": { "type": "array", "items": { "type": "string" } }
}
},
"minItems": 1,
"maxItems": 100
}Use oneOf to allow exactly one of several shapes — useful for discriminated unions like different payment methods.
{
"type": "object",
"properties": {
"payment": {
"oneOf": [
{
"type": "object",
"required": ["type", "cardNumber"],
"properties": {
"type": { "const": "card" },
"cardNumber": { "type": "string", "pattern": "^[0-9]{16}$" }
}
},
{
"type": "object",
"required": ["type", "email"],
"properties": {
"type": { "const": "paypal" },
"email": { "type": "string", "format": "email" }
}
}
]
}
}
}