JSON Schema is a declarative language that defines the structure, content, and constraints of JSON data. It enables validation, documentation, and interaction control for JSON documents by specifying expected types, required fields, and value constraints.
Key Features
Validation: Ensures JSON data adheres to a defined structure (e.g., required fields, data types, range limits).
Standardization: Maintained by the json-schema.org community, with evolving drafts (e.g., Draft 2020-12 being the latest stable version).
Self-Describing: A JSON Schema is itself a valid JSON document, using keywords like
type,properties,required,minimum,maximum, andpattern.
Common Use Cases
Validating API request/response payloads.
Generating dynamic forms in frontend frameworks (e.g., React with RJSF or JSON Forms).
Enforcing data integrity in databases (e.g., Oracle AI Database).
Automating testing and documentation.
Example Schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"description": "A person with name and age",
"type": "object",
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 18, "maximum": 64 }
},
"required": ["name", "age"]
}Tools & Libraries
Python:
jsonschemalibrary supports Draft 2020-12 and above.JavaScript/Node.js: Use
ajv(Another JSON Validator) for high-performance validation.Postman: Built-in JSON Schema validation for API testing.
Command Line: Use
check-jsonschema(installed viapipornpm) for validating JSON files.
Best Practices
Always include the
$schemakeyword to specify the draft version.Use
descriptionandtitlefor better documentation.Leverage
enumfor allowed values,constfor exact matches, andformatfor standardized formats (e.g.,email,date-time).
For more details, visit the official JSON Schema website.
Videos
The Last Breaking Change | JSON Schema Blog
Wtf is json schema
Data validation with JSON schema
JSON Schema as Source of Truth
» pip install jsonschema