Introduction
JSON Schema is a JSON document that describes what other JSON documents must look like: which properties an object has, which are required, and what type and range each value may take. A validator checks a document against the schema, and editors use schemas to offer completion and show mistakes as you type.
Schemas are saved as ordinary .json files, often named
*.schema.json. Many configuration files, such as
package.json and tsconfig.json, have published schemas, and
OpenAPI descriptions of web APIs use JSON Schema for their data types.
Structure
A schema is a JSON object, or true or false. Its keys are
keywords. Common ones are:
$schema: the URL of the version of JSON Schema the file follows, such ashttps://json-schema.org/draft/2020-12/schema$id: the schema’s own URL, and$refto point at another schema or part of onetype:object,array,string,number,integer,booleanornullpropertiesandrequiredfor objects, anditemsfor arraysenum,minimum,maxLength,patternand others that limit valuesallOf,anyOf,oneOfandnotto combine schemas
Several drafts of the standard are in use, and the $schema value says which
one a file follows. Draft-07 and 2020-12 are the most common. The
keywords and drafts page
lists every keyword and how the drafts differ.
Not described
The reference follows the 2020-12 core and validation specifications and the meta-schemas of every draft.
Known, not yet covered
- Viewer: the viewer checks a schema against the meta-schema of its dialect (2020-12, 2019-09, draft-07, 6 and 4), lists its keywords and resolves its references inside the file. It does not validate other JSON documents against the schema, and does not evaluate
unevaluatedProperties,unevaluatedItems, format assertions or the vocabularies named by$vocabulary. Draft-03 and unknown dialects are named but not validated. A reference to another document is marked as outside the file, never fetched. A schema with no$schemaopens as plain JSON, a schema in UTF-16 or UTF-32 likewise, and a schema larger than 32 MB is shown as JSON.
Unknown
- Reference: a 2020-12 Hyper-Schema specification text; json-schema.org serves only its meta-schemas, so the Hyper-Schema section takes the semantics from the 2019-09 text.
Also used for
- Browser extension manifest – The JSON manifest of a browser extension: its name, permissions and scripts.
- GeoJSON – Map points, lines and areas with properties, written as JSON.
- Hugging Face model config (config.json) – config.json: a model’s architecture and sizes, read before the weights.
- Hugging Face tokenizer (tokenizer.json) – tokenizer.json: a language model’s vocabulary and tokenising rules.
- JavaScript Object Notation – Text format for objects, arrays, strings, numbers and booleans.
- Lottie animation – Vector animations as JSON, exported from After Effects and played in apps and web pages.
- Visual Studio Code settings – VS Code’s settings.json: dotted setting IDs in JSON with comments.
- jCal: iCalendar in JSON – iCalendar as JSON: events, to-dos and time zones as nested arrays.
- jCard: vCard in JSON – vCard 4.0 contacts as JSON: names, numbers and addresses as arrays.
- npm package manifest (package.json) – A JavaScript package’s name, version, scripts and dependencies, in JSON.
Links
Software
- Visual Studio Code – free code editor that validates JSON files against their schemas (Windows, macOS, Linux)
- JSON Schema Store – a collection of schemas for common configuration files (web)
Developers
- Ajv – fast JSON Schema validator (JavaScript)
- jsonschema – JSON Schema validator (Python)
Reference
- JSON Schema specification – the drafts of the standard, on json-schema.org