DeveloperApr 7, 2026 ยท Updated Oct 13, 2026 ยท 6 min read

JSON Schema Validation: The Complete Guide

Learn JSON Schema: define data structures, validate API payloads, document contracts, and generate schemas with types, patterns, and conditional rules.

๐Ÿ“
Try the JSON Schema Generator
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01What Is JSON Schema?02Basic Type Definitions03Objects & Properties04Array Validation05String Formats & Patterns06Composition Keywords: allOf, anyOf, oneOf07Conditional Schemas08Validation Libraries09Worked Example: Seven Cases in Ajv10JSON Schema for API Documentation

What Is JSON Schema?

JSON Schema is a vocabulary for describing and validating the structure of JSON data. It defines what fields a JSON object should have, what types those fields should be, which ones are required, and what constraints apply โ€” minimum values, string patterns, enum options, and more.

Think of JSON Schema as a contract for your data. APIs use it to validate request bodies before processing them. Configuration files use it for editor autocompletion and error highlighting. Documentation generators use it to produce accurate API references. The schema itself is just JSON, making it easy to read, write, and share.

Basic Type Definitions

JSON Schema supports seven primitive types: string, number, integer, boolean, null, object, and array.

{ "type": "string" } { "type": "number", "minimum": 0, "maximum": 100 } { "type": "integer", "multipleOf": 5 } { "type": "boolean" }

Numbers support minimum, maximum, exclusiveMinimum, exclusiveMaximum, and multipleOf constraints. Strings support minLength, maxLength, pattern (regex), and format (semantic validation like email, date, URI).

Objects & Properties

Object schemas define the expected properties, their types, and which ones are required:

{ "type": "object", "properties": { "name": { "type": "string", "minLength": 1 }, "email": { "type": "string", "format": "email" }, "age": { "type": "integer", "minimum": 0 } }, "required": ["name", "email"], "additionalProperties": false }

required lists properties that must be present. additionalProperties: false rejects any fields not listed in properties โ€” useful for strict API validation. Set it to a schema to allow extra properties of a specific type.

additionalProperties only sees the properties declared next to it. Combine schemas with allOf and put additionalProperties: false in one of them, and it rejects the fields declared in the others: in Ajv 8 a valid {"name": "Ana", "age": 30} failed that way. Draft 2019-09 added unevaluatedProperties: false for this case; placed at the top level it accepted the same object and still rejected an extra field.

Array Validation

Array schemas control item types, length, and uniqueness:

{ "type": "array", "items": { "type": "string" }, "minItems": 1, "maxItems": 10, "uniqueItems": true }

For tuple validation (arrays with a fixed sequence of differently-typed items), use prefixItems:

{ "type": "array", "prefixItems": [ { "type": "number" }, { "type": "string" }, { "type": "boolean" } ] }

This validates that the first item is a number, the second a string, and the third a boolean.

String Formats & Patterns

The format keyword names a semantic type for a string. One catch: in draft 2019-09 and 2020-12, format is only an annotation unless the validator turns on format checking. In our test Ajv 8 refused to compile {"format": "email"} in its default strict mode, accepted "not-an-email" with strict mode off, and only rejected it once the ajv-formats plugin was added. Check how your validator handles formats before relying on them:

email, uri, date-time, date, time, ipv4, ipv6, uuid, hostname, json-pointer, and regex.

For custom patterns, use the pattern keyword with a regular expression:

{ "type": "string", "pattern": "^[A-Z]{2}-[0-9]{4}$" }

This validates strings like "US-1234" or "GB-5678". Combine format and pattern for layered validation โ€” format catches the general shape, pattern enforces specific structure.

Composition Keywords: allOf, anyOf, oneOf

Composition keywords combine multiple schemas:

allOf โ€” the data must match all listed schemas. Used for extending a base schema with additional constraints.

anyOf โ€” the data must match at least one schema. Used for fields that accept multiple types.

oneOf โ€” the data must match exactly one schema (not zero, not more than one). Used for discriminated unions.

{ "oneOf": [ { "type": "object", "properties": { "type": { "const": "email" }, "address": { "type": "string", "format": "email" } } }, { "type": "object", "properties": { "type": { "const": "sms" }, "phone": { "type": "string", "pattern": "^\\+[0-9]+$" } } } ] }

This validates a notification channel that's either an email (with address) or an SMS (with phone), but not both.

Conditional Schemas

The if/then/else keywords enable conditional validation:

{ "type": "object", "properties": { "payment_method": { "enum": ["credit_card", "bank_transfer"] } }, "if": { "properties": { "payment_method": { "const": "credit_card" } } }, "then": { "required": ["card_number", "expiry"] }, "else": { "required": ["account_number", "routing_number"] } }

If payment_method is "credit_card", then card_number and expiry are required. Otherwise, account_number and routing_number are required. This is cleaner than duplicating the entire schema with oneOf.

Validation Libraries

Every major language has JSON Schema validation libraries:

JavaScript/TypeScript: AJV (the fastest and most popular), Zod (TypeScript-first schema library that can export JSON Schema).

Python: jsonschema (reference implementation), fastjsonschema (compiled validation for performance).

Go: gojsonschema. Java: everit-org/json-schema. PHP: justinrainbow/json-schema.

Most API frameworks integrate JSON Schema validation directly. Fastify validates request bodies with Ajv built in; Express has no built-in validation, so it is added with middleware that wraps Ajv. FastAPI uses Pydantic models that generate JSON Schema automatically. Spring Boot supports JSON Schema validation through annotations.

Worked Example: Seven Cases in Ajv

We ran each case in Ajv 8.17.1 with its draft 2020-12 class, with and without the ajv-formats 3.0.1 plugin (October 14, 2026; script in our test folder):

Schema and dataAjv 8.17.1 result
{"type":"string","format":"email"} with "not-an-email", Ajv defaultsrefuses to compile: unknown format (strict mode)
same, strict mode off, no ajv-formatsvalid (format ignored)
same, with ajv-formatsinvalid; "a@b.co" valid
allOf + additionalProperties:false, {"name","age"}invalid
allOf + unevaluatedProperties:false, same objectvalid; extra field rejected
{"type":"integer"} with 1.0valid (JSON Schema treats 1.0 as an integer)
{"required":["id"]} with {"id": null}valid: required means present, not non-null

The last two surprise people most. A validator sees parsed values, so 1.0 and 1 are the same number and both pass "integer"; if the literal text matters, check it before parsing. And required only means the key is present: add "not": {"type": "null"} or a type without "null" to the property itself to forbid null.

JSON Schema for API Documentation

OpenAPI (Swagger) specifications use JSON Schema to describe request and response bodies. Your schema definitions serve double duty: they validate incoming data and generate interactive API documentation.

Tools like Swagger UI, Redoc, and Stoplight render JSON Schema into browsable documentation with type information, constraints, and example values. When your schema changes, the documentation updates automatically.

The JSON Schema Generator tool can create schemas from example JSON data โ€” paste an API response and get a draft schema you can refine. This is much faster than writing schemas by hand for complex nested objects.

Frequently Asked Questions

What is JSON Schema?+
JSON Schema is a vocabulary for defining the structure and validation rules of JSON data. It specifies field types, required properties, value constraints, and patterns. It's written in JSON itself and used for API validation, documentation, and code generation.
How do I validate JSON against a schema?+
Use a JSON Schema validation library for your language โ€” AJV for JavaScript, jsonschema for Python, gojsonschema for Go. Pass your data and schema to the validator, and it returns whether the data conforms and what errors exist.
What is the difference between anyOf and oneOf?+
anyOf passes if the data matches at least one of the listed schemas. oneOf passes only if the data matches exactly one schema โ€” if it matches two or more, validation fails. Use oneOf for discriminated unions where exactly one variant should match.
Can JSON Schema validate nested objects?+
Yes. You define nested schemas by placing object schemas inside the properties of a parent object schema. You can also use $ref to reference shared schema definitions and avoid duplication.
Try it yourself

Use the JSON Schema Generator โ€” free, no signup required.

โšก Open JSON Schema Generator
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.
โšก Try the free JSON to TypeScript Converter โ†’