What is JSON Schema?
JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. It describes the structure, constraints, and documentation of JSON data, enabling automated validation and documentation generation.
Quick Facts
| Full Name | JSON Schema Specification |
|---|---|
| Created | 2009 (first draft) |
| Specification | Official Specification |
How JSON Schema Works
JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. It can validate data types, required properties, string patterns, numeric ranges, array lengths, and more. JSON Schema is written in JSON itself, making it easy to understand and generate. It supports features like $ref for reusable definitions, allOf/anyOf/oneOf for composition, and conditional validation with if/then/else. JSON Schema is widely used in API documentation (OpenAPI), configuration validation, and form generation.
Key Characteristics
- Written in JSON format
- Validates data types, formats, and constraints
- Supports $ref for reusable definitions
- Composition with allOf, anyOf, oneOf
- Conditional validation with if/then/else
- Used in OpenAPI/Swagger specifications
Common Use Cases
- API request/response validation
- Configuration file validation
- Form generation and validation
- Documentation generation
- Data interchange contracts
Example
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"name": {
"type": "string",
"minLength": 1,
"maxLength": 100
},
"email": {
"type": "string",
"format": "email"
},
"age": {
"type": "integer",
"minimum": 0,
"maximum": 150
},
"tags": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true
}
},
"required": ["id", "name", "email"],
"additionalProperties": false
}