What is JSON Schema?
JSON Schema is a vocabulary that allows you to validate, annotate, and manipulate JSON documents. It defines a declarative language for creating schemas that describe the structure, constraints, and metadata associated with JSON data.Core Terminology
Instance
A JSON document to which a schema is applied is known as a JSON instance or just an instance. An instance can be any valid JSON value as defined by RFC 8259:null- A JSON null valueboolean- A true or false valueobject- An unordered set of properties mapping strings to instancesarray- An ordered list of instancesnumber- An arbitrary-precision, base-10 decimal number valuestring- A string of Unicode code points
JSON Schema is programming language agnostic and supports the full range of JSON values. However, some languages may not be able to represent the full range of values describable by JSON.
Schema
A JSON Schema document (or simply a schema) is used to describe an instance. A schema can itself be interpreted as an instance, since it is also valid JSON. A JSON Schema MUST be an object or a boolean.Schema Objects and Keywords
Object properties that are applied to the instance are called keywords or schema keywords. Keywords fall into several categories:- Identifiers - Control schema identification through setting an IRI for the schema (e.g.,
$id,$schema) - Assertions - Produce a boolean result when applied to an instance (e.g.,
type,minimum,required) - Annotations - Attach information to an instance for application use (e.g.,
title,description,default) - Applicators - Apply one or more subschemas to parts of the instance (e.g.,
properties,items,allOf) - Reserved locations - Reserve a place for a specific purpose like reusable schemas (e.g.,
$defs)
Boolean Schemas
The boolean schema valuestrue and false are trivial schemas that always produce themselves as assertion results:
true- Always passes validation (equivalent to an empty schema{})false- Always fails validation (equivalent to{"not": {}})
How Validation Works
A JSON Schema represents a set of constraints and annotations that are applied to a JSON value. An instance is considered valid against a schema if it satisfies the constraint defined by every keyword in that schema.Schema evaluation is a recursive process. Some keywords contain subschemas that can be used to create complex constraints or describe compound values like arrays and objects.
Example: Object Validation
To describe a JSON object, a schema uses:- The
typekeyword to declare that the value MUST be an object - The
propertieskeyword to apply separate schemas to each property - The
requiredkeyword to specify mandatory properties
- Must have a
nameproperty (string) - May have an
ageproperty (non-negative integer) - Can have additional properties
Primary Use Cases
1. API Validation
Validate request and response payloads in REST APIs to ensure data integrity:2. Configuration Files
Define and validate application configuration structures:3. Documentation Generation
Use annotations to generate human-readable documentation:4. Form Generation
Annotations can drive UI form builders:Instance Equality
Two JSON instances are equal if and only if they are of the same type and have the same value according to the data model:- Both are null; or
- Both are true; or
- Both are false; or
- Both are strings, and are the same codepoint-for-codepoint; or
- Both are numbers, and have the same mathematical value; or
- Both are arrays, and have an equal value item-for-item; or
- Both are objects, and each property in one has exactly one property with a key equal to the other’s, and that property has an equal value
Keyword Behaviors
JSON Schema keywords exhibit one or more behaviors:Assertions
Assertions produce a boolean result when evaluating an instance:true.
Annotations
Annotations attach information to instance locations:Applicators
Applicators apply subschemas to parts of the instance:properties and items keywords are applicators that apply subschemas to object properties and array items respectively.
Assertion and Instance Types
Most assertions only constrain values within a certain primitive type. When the instance type doesn’t match the keyword’s target type, the instance is considered valid against that assertion.
maxLength keyword only restricts strings. If the instance is a number, boolean, null, array, or object, it’s valid against this assertion:
- A string with at most 255 characters
- A null value
maxLength also restricted the instance type to be a string, this would be much more cumbersome to express.
Meta-Schemas
A meta-schema is a schema that describes a schema. Meta-schemas validate JSON Schemas and specify the set of keywords those schemas can use. Every schema has a meta-schema, declared using the$schema keyword:
Schema Resources and Identification
A JSON Schema resource is a schema canonically identified by an absolute IRI (without fragments). The$id keyword is used to identify schemas:
The
$id IRI is an identifier and not necessarily a network locator. A schema need not be downloadable from its canonical IRI.Next Steps
Now that you understand the fundamentals, try creating your first schema:Getting Started
Write a simple schema and validate JSON data against it