JSON guide · 7 min read
How to Validate JSON Against a JSON Schema
Validate JSON structure and values against a schema, interpret errors and test required fields, types and constraints.
Syntax validation is not schema validation
A JSON document can be syntactically valid but still violate an application contract. Schema validation checks expected types, required properties, patterns and other constraints.
Read validation errors by path
Each error should identify the failing instance path and the violated schema keyword. Fix the first structural errors before interpreting dependent errors lower in the document.
Common schema rules
Use only constraints that represent real business or interface requirements.
- type controls the expected JSON type.
- required lists mandatory object properties.
- properties defines rules for named fields.
- items defines array element rules.
- minimum, pattern and enum narrow accepted values.
Test both passing and failing examples
A useful contract test includes valid samples and deliberate failures for missing fields, wrong types, invalid enum values and boundary conditions.
Common questions
Frequently asked questions
Why does valid JSON fail schema validation?
The syntax is valid, but one or more values do not meet the structure or constraints declared by the schema.
Can JSON Schema validate business logic?
It handles many structural rules, but cross-field or external business rules may still require application code.