What is a Schema?
A JSON Schema document, or simply a schema, is used to describe an instance. A schema can itself be interpreted as an instance, making JSON Schema self-descriptive.A JSON Schema MUST be an object or a boolean.
Schema Objects and Keywords
When a schema is an object, its properties are called keywords (or schema keywords). Keywords broadly fall into five categories:- Identifiers: Control schema identification through setting an IRI for the schema and/or changing how the base IRI is determined
- Assertions: Produce a boolean result when applied to an instance
- Annotations: Attach information to an instance for application use
- Applicators: Apply one or more subschemas to a particular location in the instance, and combine or modify their results
- Reserved locations: Do not directly affect results, but reserve a place for a specific purpose to ensure interoperability
Keywords which are properties within the same schema object are referred to as adjacent keywords.
Boolean Schemas
The boolean schema valuestrue and false are trivial schemas that always produce themselves as assertion results, regardless of the instance value.
true: Always passes validation, as if the empty schema{}false: Always fails validation, as if the schema{ "not": {} }
{} is unambiguous, there are many possible equivalents to the false schema. Using boolean values ensures that the intent is clear to both human readers and implementations.
Core Schema Keywords
Three essential keywords identify and describe schemas:The $schema Keyword
The $schema keyword is both used as a JSON Schema dialect identifier and as the identifier of a resource which is itself a JSON Schema (a meta-schema).
The $id Keyword
An $id keyword in a schema identifies that schema as a distinct schema resource. The value MUST be a string representing a valid IRI reference without a fragment.
The root schema of a JSON Schema document SHOULD contain an
$id keyword with an absolute IRI (containing a scheme, but no fragment).$id keyword serves multiple purposes:
- Identifies the schema resource with a canonical IRI
- Establishes the base IRI for relative references within the schema
- Enables references from other schemas
The $vocabulary Keyword
The $vocabulary keyword is used in meta-schemas to declare which vocabularies are available for use. Its value MUST be an object where each key is a vocabulary IRI and each value is a boolean.
true indicates the vocabulary is required, while false indicates it is optional.
Meta-Schemas
A meta-schema is a schema that describes other schemas. Meta-schemas are used to:- Validate JSON Schemas
- Specify the set of keywords those schemas are using
- Define the dialect of JSON Schema being used
- Meta-Schema Example
- Using a Meta-Schema
The official JSON Schema meta-schema defines the structure of valid schemas:
Dialect Determination
When evaluation encounters a new schema resource, the dialect is determined using these prioritized steps:- The
$schemakeyword - Implementations MUST process the schema according to the dialect it declares - Parent schema dialect - An embedded schema resource without
$schemauses the same dialect as its parent - External context - Default dialect from media type or embedding document
- User configuration - Implementation-specific dialect configuration
Root Schemas, Subschemas, and Resources
A root schema is the top-level schema identified by an absolute IRI. It comprises the entire JSON document, recursively including all of its subschemas.- The schema with title “root” is the root schema
- The schema with title “array item” is a subschema
- Both the root and any subschemas can be objects or booleans
Some keywords take schemas themselves, allowing JSON Schemas to be nested. The schema titled “array item” is a subschema of the root schema.
Embedded Schema Resources
A JSON Schema document can contain multiple schema resources. An embedded resource is identified by an$id keyword within the document:
https://example.com/user.json is an embedded schema resource within the root resource.
Schema Structure Example
Here’s a complete example showing schema structure with identification:- Declares its dialect with
$schema - Identifies itself with
$id - Uses annotation keywords (
title,description) - Uses assertion keywords (
type,minimum,required) - Uses applicator keywords (
properties)