How to Validate JSON Against a JSON Schema
Step-by-step guide to validating JSON documents against JSON Schema — draft versions, common keywords, and worked examples for API payloads.
JSON Schema describes the shape of JSON you expect: required fields, types, enums, nested objects, and arrays. Validating instance documents against a schema catches contract drift before it hits production clients.
JSON Schema basics
A schema is itself a JSON document. Common meta-schema drafts:
- Draft 2020-12 — current
$schemafor new work - Draft-07 — still common in OpenAPI 3.0 tooling
Always set "$schema" in your schema file so validators pick the right ruleset.
Core keywords:
type—string,number,integer,boolean,object,array,nullproperties/required— object fieldsitems— array element schemaenum,const— allowed valuesminimum,maximum,pattern,format— constraints
Example: user profile schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["id", "email"],
"properties": {
"id": { "type": "string", "format": "uuid" },
"email": { "type": "string", "format": "email" },
"role": { "type": "string", "enum": ["member", "admin"] },
"tags": {
"type": "array",
"items": { "type": "string" },
"maxItems": 10
}
},
"additionalProperties": false
}
additionalProperties: false rejects surprise keys — useful for strict API contracts.
Example: validating an instance
Valid instance:
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"email": "dev@example.com",
"role": "member",
"tags": ["api"]
}
Invalid instance (missing email, wrong type on role):
{
"id": 123,
"role": "superuser"
}
A good validator reports paths to each error: /id expected string, /email required, /role not in enum.
Workflow for API teams
- Author schema from real response samples or OpenAPI
components.schemas. - Validate golden fixtures in CI — one valid file per endpoint, plus negative cases.
- Version schemas alongside API versions (
v1/user.schema.json). - On failure, diff the instance against last-known-good to see what changed.
OpenAPI and JSON Schema
OpenAPI 3.x embeds JSON Schema (mostly Draft-07 subset) for request/response bodies. The same schema file can power:
- Documentation examples
- Server-side validation (framework-dependent)
- Client code generation
Watch for nullable (OAS 3.0) vs type: ["string", "null"] (plain JSON Schema).
Common validation pitfalls
integervsnumber—1.0failsintegertype.- Optional vs required — omitting a key is fine unless listed in
required. formatvalidation — not all validators enforceformat; check your engine.- Unevaluated properties — Draft 2020-12 uses
unevaluatedPropertiesin combinator cases.
Validate in BracketView
Paste your schema and instance into the JSON Schema Validator. Fix syntax errors in either document with the built-in JSON validator first, then read structured error output with JSON Pointer paths.
See also: JSON Schema glossary term and type generation from JSON.
Try this in BracketView
Open the BracketView workspace — core tools run in your browser.
Related BracketView tools
Related articles
- Debugging Malformed API Responses: A Step-by-Step Guide
When an API returns broken JSON, use this workflow — capture, isolate, validate, compare to schema, and fix at the source — without guessing at syntax errors.
- Common JSON Syntax Errors and How to Fix Them
Unexpected token, trailing commas, unescaped strings — learn what causes the most common JSON parse errors and how to fix them with a validator workflow.
- Understanding JSON Diff: Structural vs Value-Level Comparisons
JSON diff tools can report added keys, removed branches, and changed values differently. Learn comparison modes and how to interpret diff output for API reviews.