Skip to content

JSON Schema: A Complete Guide to Validating JSON Data

JSON Schema is a declarative language that lets you describe the structure and constraints of JSON data. It acts as a contract between systems, ensuring that the JSON exchanged between APIs, configuration files and databases matches an expected shape. Whether you are validating user input, generating documentation or building form UIs from data models, JSON Schema gives you a standard, machine-readable way to define what valid JSON looks like.

What is JSON Schema?

JSON Schema itself is written in JSON. It uses a set of keywords to describe the expected types, properties, constraints and formats of a JSON document. The most widely used version is Draft-07, identified by the $schema keyword. Here is a simple example:

{

"$schema": "http://json-schema.org/draft-07/schema#",

"type": "object",

"properties": {

"name": { "type": "string" },

"age": { "type": "integer" },

"email": { "type": "string", "format": "email" }

},

"required": ["name", "email"]

}

This schema says the JSON must be an object with a string name, an integer age and a string email in email format. The name and email fields are required, while age is optional.

JSON Schema Types

JSON Schema supports seven primitive types. Each maps directly to a JSON value type:

string → "hello", "2026-01-01"

number → 3.14, -42.5

integer → 1, 42, -7

boolean → true, false

null → null

object → { "key": "value" }

array → [1, 2, 3]

The integer type is a subset of number. A value of 42 matches both, but 42.5 only matches number.

Describing Objects with Properties

Objects are the most common type in JSON Schema. You define their shape using the properties keyword, and control which fields are mandatory with required:

  • properties lists each field and its sub-schema (type, format, constraints).
  • required is an array of field names that must be present.
  • additionalProperties controls whether extra fields are allowed. Setting it to false rejects any field not listed in properties.

Working with Arrays

Arrays are described using the items keyword, which defines the schema for each element. You can also use minItems and maxItems to constrain length, and uniqueItems to prevent duplicates:

{

"type": "array",

"items": { "type": "string" },

"minItems": 1,

"uniqueItems": true

}

String Formats

JSON Schema includes built-in format annotations for common string patterns. These do not enforce validation by default in all implementations, but schema generators and validators typically recognize them:

date-time → 2026-01-15T10:30:00Z (ISO 8601)

date → 2026-01-15

email → user@example.com

uri → https://example.com/page

uuid → 550e8400-e29b-41d4-a716-446655440000

ipv4 → 192.168.1.1

ipv6 → ::1

Nested Objects and Complex Structures

Real-world JSON often contains deeply nested objects. JSON Schema handles this naturally by nesting schemas within properties. Each nested object gets its own type, properties and required definition. For example, a user object with a nested address:

"address": {

"type": "object",

"properties": {

"street": { "type": "string" },

"city": { "type": "string" },

"zip": { "type": "string" }

},

"required": ["street", "city"]

}

Generating Schema from JSON Automatically

Writing JSON Schema by hand is tedious, especially for large or deeply nested JSON structures. Schema generators analyze a sample JSON document and produce a matching schema automatically. A good generator will:

  • Detect types correctly. Strings, numbers, integers, booleans, nulls, objects and arrays are identified from the values.
  • Recognize string formats. Values that look like emails, URLs, dates or UUIDs get the appropriate format annotation.
  • Handle arrays smartly. If an array contains objects, the generator merges schemas from multiple elements to produce a single, unified item schema.
  • Mark required fields. Fields that appear in the sample data are marked as required (optionally).

Common Use Cases

  • API validation. Validate request and response bodies against schemas in Express, FastAPI or any framework that supports JSON Schema validators like Ajv or jsonschema.
  • Configuration files. Define the expected structure of config files so that typos and missing fields are caught before runtime.
  • Form generation. Libraries like react-jsonschema-form can render entire forms from a JSON Schema definition, reducing boilerplate code.
  • Documentation. JSON Schema serves as living documentation for your data models, always in sync with the validation logic.
  • Code generation. Tools can generate TypeScript interfaces, Go structs or Python dataclasses from JSON Schema definitions.

Best Practices

  • Start from real data. Generate your initial schema from actual API responses or sample data, then refine it manually.
  • Use additionalProperties: false. This catches typos and unexpected fields early, preventing silent data corruption.
  • Add format annotations. Mark emails, dates and URIs so validators and documentation tools can provide richer feedback.
  • Keep schemas in version control. Treat schemas as code. Review changes, track history and share them across teams.
  • Test with edge cases. Validate your schema against empty objects, null values, missing fields and extra properties to make sure it rejects what it should.

Generate JSON Schema from your data

Paste any JSON object or array and get a complete JSON Schema (Draft-07) with type detection, format recognition and required fields. All processing happens in your browser.

Open JSON Schema Generator