JSON Schema
JSON Schema is a vocabulary for describing the structure of JSON documents, defining types, constraints, and validation rules; it is the schema standard for JSON.
Purpose
To give JSON a validation mechanism similar to XSD, ensuring data matches the expected structure.
Mechanism
A schema describes itself in JSON: type declares the type, properties declares object properties, items declares array elements, required declares mandatory fields, and minimum/maximum/pattern declare constraints. Draft 2020-12 is the latest version. Validators (ajv) check documents against it.
Use cases
- API request/response validation
- Configuration file checking
- Form data validation
- OpenAPI type definitions
Examples
- Input
{"type":"object","properties":{"age":{"type":"integer","minimum":0}}}OutputValidates that age is a non-negative integerConstraint declaration
Common errors
- Using an outdated draft version breaks validator compatibility
- Pin the draft version and use a matching validator
Related terms
XSD
XSD (XML Schema Definition) is the W3C-standard language for defining XML document structure, describing elements, attributes, and type constraints in XML syntax. GLOSSARY
DTD
A DTD (Document Type Definition) is the structure-definition mechanism of XML 1.0; it is simpler than XSD but limited, and is written in a special (non-XML) syntax. GLOSSARY
JSON
JSON (JavaScript Object Notation) is a lightweight data interchange format based on JavaScript object literals, using key-value pairs and arrays. GLOSSARY