JSON Schema Validation Explained

What JSON Schema is, how to validate data against a schema, common patterns, and how to debug invalid JSON.

JSON Schema is a vocabulary that allows you to annotate and validate JSON data. It defines the structure, types, and constraints of your data so that APIs, configuration files, and user inputs can be automatically checked for correctness. If you work with APIs or any data-driven application, understanding JSON Schema is essential.

What is JSON Schema?

A JSON Schema is itself a JSON document that describes the expected shape of another JSON document. It specifies which fields must exist, what types they should be, what values are acceptable, and how nested structures should look. Think of it as a contract between the producer and consumer of data.

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

This schema says: the data must be an object with a required name (string), an optional age (non-negative integer), and a required email (string in email format). If any constraint fails, validation produces an error describing exactly what went wrong.

Why use JSON Schema?

Without validation, your application silently accepts any data and fails unpredictably at runtime. JSON Schema catches errors early — at the API boundary, during configuration loading, or before database writes. The benefits include:

  • API contracts — Ensure request and response bodies match the expected format. Tools like OpenAPI use JSON Schema under the hood.
  • Configuration validation — Catch typos and missing fields in config files before they cause outages.
  • Form validation — Validate user input on both client and server using the same schema.
  • Documentation — A well-written schema serves as living documentation of your data model.

Core keywords

JSON Schema has several categories of keywords. The most important ones are:

Type keywords: type specifies the expected data type — "string", "number", "integer", "boolean", "array", "object", or "null".

String keywords: minLength, maxLength, pattern (regex), and format (predefined formats like "email", "uri", "date").

Numeric keywords: minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf.

Array keywords: items (schema for each element), minItems, maxItems, uniqueItems.

Object keywords: properties, required, additionalProperties, minProperties, maxProperties.

Composition with allOf, anyOf, oneOf

Real-world schemas often combine multiple constraints. JSON Schema provides composition keywords for this:

{  "anyOf": [    { "type": "string" },    { "type": "number" }  ]}

This accepts either a string or a number. allOf requires all schemas to match (intersection). oneOf requires exactly one schema to match. not inverts a schema. These let you build complex validation logic from simple building blocks.

Debugging invalid JSON

When validation fails, you need clear error messages. Most JSON Schema validators produce detailed output with the path to the invalid field, the failing constraint, and the actual value. The JSON Formatter tool can help you first format and inspect your JSON before validating it.

Common debugging tips: start by checking that your JSON is valid (no trailing commas, no single quotes, no unquoted keys). Then verify the data types match — a string where a number is expected is the most frequent error. Finally, check required fields are present.

Best practices

  • Use $ref — Avoid duplicating schemas. Define reusable schemas and reference them with $ref.
  • Be specific — Use enum for fixed sets of values, pattern for string formats, and const for exact matches.
  • Version your schemas — Use $schema and $id to track schema versions.
  • Validate at boundaries — Check data as early as possible — at API entry points and configuration loading.
  • Don't over-validate — A schema that is too strict becomes a burden. Focus on constraints that prevent real bugs.

Try it

Use a local tool to validate your JSON data against a schema — everything runs in your browser, no data leaves your machine.

Frequently asked questions

What is JSON Schema?
JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. It defines the structure, types, required fields, and constraints of your JSON data. Think of it as a contract that describes what valid data looks like.
Is JSON Schema the same as validation?
No. JSON Schema is the specification; validation is the process of checking data against a schema. Many tools implement JSON Schema validation, but the schema itself is just a description.
Can JSON Schema validate JSON against an API specification?
Yes. JSON Schema is commonly used in OpenAPI/Swagger specifications to define request and response payloads. Tools like the JSON Validator on DevSpeedTools can validate your data against a schema instantly.
How do I debug a JSON Schema validation error?
Most validators return the exact path where validation failed. Check the error message for the property name and the constraint that was violated. Common issues: missing required fields, wrong types, and string format violations.