JSON Schema
JSON Schema is a standard to define and validate the structure and constraints of JSON data.
- Configuration files
- API responses
- Language agnostic types
JSON Schema Format
{ "$schema": "https://json-schema.org/draft/2020-12/schema", Advanced โ 11 collapsed lines
"$vocabulary": { "https://json-schema.org/draft/2020-12/vocab/core": true, "https://json-schema.org/draft/2020-12/vocab/applicator": true, "https://json-schema.org/draft/2020-12/vocab/validation": true, "https://json-schema.org/draft/2020-12/vocab/unevaluated": true, "https://json-schema.org/draft/2020-12/vocab/format-annotation": true, "https://json-schema.org/draft/2020-12/vocab/format-assertion": true, "https://json-schema.org/draft/2020-12/vocab/content": true, "https://json-schema.org/draft/2020-12/vocab/meta-data": true, ... โ "$vocabulary" allows to extend JSON schema with custom keywords
JSON Schema Docs โ Vocabularies list
Since Draft 2019-09
}, "$id": "https://stephcraft.net/schema/demo.json", โThe URL at which the schema is hosted ยท optional
JSON Schema Docs โ Unique identifier
"title": "Demo", "description": "learn JSON schema at https://stephcraft.net/docs/json-schema", โThe title and description of the schema ยท optional
Example schema โ 17 collapsed lines
"properties": { "value": { "type": "number" }, "enabled": { "type": "boolean" }, "text": { "type": "string" }, "nothing": { "type": "null" }, "zero": { "const": 0 }, "category": { "enum": ["release", "draft"] }, "tags": { "type": "array" }, "nested": { "type": "object", "properties": { "first": { "type": "string" }, "last": { "type": "string" } } } }, "required": ["value", "enabled"] }Example Usage
JSON Schema is supported by JSON ยท YAML ยท TOML
JSON
{ "$schema": "./schema.json", "value": 115, "enabled": true, "text": "hello world", "nothing": null, "zero": 0, "category": "stable", "tags": ["cool", "awesome", "fantastic"], "nested": { "first": "Gordon", "last": "Freeman" }} YAML
# $schema: ./schema.jsonvalue: 115enabled: truetext: hello worldtags: [cool, awesome, fantastic]nothing: nullzero: 0category: stablenested: first: Gordon last: Freeman TOML
"$schema" = "./schema.json"value = 115enabled = truetext = "hello world"nothing = "null"zero = 0category = "stable"tags = ["cool", "awesome", "fantastic"]
[nested]first = "Gordon"last = "Freeman"Versions
| Draft 2020-12 | "https://json-schema.org/draft/2020-12/schema" |
Jun 2022 | Latest |
| Draft 2019-09 | "https://json-schema.org/draft/2019-09/schema" |
Sep 2019 |
Legacy Drafts
| Draft 7 | "https://json-schema.org/draft/draft-07/schema" |
Mar 2018 |
| Draft 6 | "https://json-schema.org/draft/draft-06/schema" |
Apr 2017 |
| Draft 4 | "https://json-schema.org/draft/draft-04/schema" |
Oct 2016 |
| Draft 3 | "https://json-schema.org/draft/draft-03/schema" |
Nov 2010 |
| Draft 2 | "https://json-schema.org/draft/draft-02/schema" |
Mar 2010 |
| Draft 1 | "https://json-schema.org/draft/draft-01/schema" |
Dec 2009 |
| Draft 0 | "https://json-schema.org/draft/draft-00/schema" |
Dec 2009 |
Types
Const
Enum
{ "enum": ["pink", "blurple", false, null, ...] } Value must be one of the specified options
JSON Schema Docs โ enum
Null
Boolean
Number
{ "type": "number", "type" can be either:
"number" Value must be a number 1.0
"integer" Value must be a whole number 1
"minimum": 0, "maximum": 100, "exclusiveMinimum": 0, "exclusiveMaximum": 100, Range constraints can be defined
"minimum" โฅ value โฅ "maximum"
"exclusiveMinimum" > value > "exclusiveMaximum"
}String
{ "minLength": 1, "pattern": "^[a-z]+$" โ Regex constraint can be defined
JSON Schema Docs โ string regex
Regular Expressions
}Array
{ "minItems": 1, "items": { "type": "number", ... }, โ Type constraint can be defined for the array items
JSON Schema Docs โ array items
Advanced โ 11 collapsed lines
"contains": { "type": "number", ... }, "minContains": 1, "maxContains": 3, โAlternatively, Type constraints can be defined for a certain amount of items
JSON Schema Docs โ array contains
"prefixItems": [ { "type": "string", ... }, { "type": "number", ... }, { "type": "boolean", ... }, ... ], "items": false, โAlternatively, Type constraints can be defined for each of the items
- Setting
"items" to true only applies the schema for the first items and allows additional items
- Setting
"items" to false applies the schema to all items and doesn't allow additional items
"unevaluatedItems": false Additional items constraint outside of "items", "contains" and "prefixItems" can be defined
JSON Schema Docs โ array unevaluated items
}Object
{ "properties": { "id": { "type": "string", ... }, "enabled": { "type": "boolean", ... }, "value": { "type": "number", ... }, }, "required": ["id", "enabled", ...], Required properties constraints can be defined
JSON Schema Docs โ object required properties
Advanced โ 12 collapsed lines
"minProperties": 1, "patternProperties": { "^S_": { "type": "string", ... }, "^N_": { "type": "number", ... }, ... โ Regex property pattern constraints can be defined
JSON Schema Docs โ object pattern properties
}, "propertyNames": { "pattern": "^[A-Za-z_][A-Za-z0-9_]*$" โ Regex property name constraint can be defined
JSON Schema Docs โ object property names
}, "unevaluatedProperties": false, โ Additional properties constraint outside of "properties" and "patternProperties" can be defined
JSON Schema Docs โ object unevaluated properties
"additionalProperties": { "type": "string", ... }, Additional properties Type constraint can be defined
JSON Schema Docs โ object additional properties
} Dependent properties
{ "type": "object", "dependentRequired": { โA map of properties that when present, applies further required properties
JSON Schema Docs โ Dependent required
"username": [ "handle", "avatar", ... ], ... }, "dependentSchemas": { A map of properties that when present, applies further schemas
JSON Schema Docs โ Dependent Schemas
"username": {Example schema โ 5 collapsed lines
"properties": { "handle": { "type": "string", "pattern": "@.+" }, "avatar": { "type": "string", "pattern": "http.+" } }, "required": [ "handle", "avatar" ] }, ... }}Features
Annotations
Annotations state the intent of the schema, they donโt add any constraints to the data being validated.
{ "title": "...", "default": null, "deprecated": true, "readOnly": true,} String Content Annotations
String Media
{ "type": "string", "contentMediaType": "image/png", Media Type can be any MIME type
JSON Schema Docs โ Media content media type
"contentEncoding": "base64", โ Encoding can be one of:
}String Schema
{ "type": "string", "contentMediaType": "application/json", When used with a content schema, Media Type can be one of:
"application/json" Supported
"application/yaml" Unofficial
"application/toml" Unofficial
"contentSchema": { โA JSON Schema can be specified for the string's content
JSON Schema Docs โ Media content schema
Example schema โ 5 collapsed lines
"type": "object", "properties": { "alg": { "type": "string" }, "typ": { "const": "JWT" } } }}Conditional Schema
{ "type": "object", "if": {Example schema โ 3 collapsed lines
"properties": { "enabled": { "const": true } โEquivalent to if enabled == true
} }, "then": {Example schema โ 3 collapsed lines
"properties": { "text": { "minLength": 15 } } }, "else": {Example schema โ 3 collapsed lines
"properties": { "text": { "minLength": 1 } } } โConditionally apply schemas
if that schema is validated
then that schema is applied
else that schema is applied ยท optional
}Boolean Schema composition
{Example schemas โ 2 collapsed lines
{ "$comment": "This is a comment" }, { "$comment": "And this is another comment" } ],Example schemas โ 8 collapsed lines
{ "$comment": "default suggestions", "enum": [ "html", "svg", "math" ] }, { "$comment": "enum is not exhaustive, allow custom values", "type": "string", } ],Example schemas โ 9 collapsed lines
{ "properties": { "x": { "type": "number" }, "y": { "type": "number" }, "z": { "type": "number" } }, "required": [ "x", "y", "z" ] }, { "type": "number" } ],Example schema โ 3 collapsed lines
"license_plate": { "enum": [ "NULL", "DROP", "DELETE" ] } }}Modular Schema composition
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://example.com/schemas/schema.json", โRelative file path references are resolved relative to the base URL of the root schema "$id"
https://example.com
"type": "object", "properties": { },
"geolocation": {Example schema โ 5 collapsed lines
"type": "object", "properties": { "longitude": { "type": "number" }, "latitude": { "type": "number" } } }, "vector": { "$id": "https://example.com/schemas/vector.json", โSpecifying "$id" in a subschema indicates an embedded schema
- Embedded schemas can be referenced as if they were external schemas
Example schema โ 6 collapsed lines
"type": "object", "properties": { "x": { "type": "number" }, "y": { "type": "number" }, "z": { "type": "number" } } }, "types": { "$id": "https://example.com/schemas/types.json", "type": "object", "properties": { "url": {Example schema โ 2 collapsed lines
"type": "string", "pattern": "http.+" }, "email": { "$anchor": "email", โSpecifying "$anchor" allows to reference subschemas using:
- the anchor alias ยท
#id
- instead of its schema path ยท
#/properties/...
Example schema โ 2 collapsed lines
"type": "string", "pattern": "^.+@.+\\..+$" } } }, ... }}