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 Implementations

IDE Autocomplete

VS Code
Visual Studio
IntelliJ

JSON Schema libraries

JSON Schema โ€” Tools

JSON Schema Format

schema.json
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
โ€‰

"$schema" specifies the version of the JSON Schema standard

JSON Schema Docs โ€” $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

"type": "object",

"type" can be one of:

Alternatively, enum or const can be used

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

data.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

data.yml
# $schema: ./schema.json
value: 115
enabled: true
text: hello world
tags: [cool, awesome, fantastic]
nothing: null
zero: 0
category: stable
nested:
first: Gordon
last: Freeman

TOML

data.toml
"$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"

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

JSON Schema Docs โ€” Drafts

Types

Const

schema.json
{ "const": "zero" }
โ€‰

Value must be it
JSON Schema Docs โ€” const

Enum

schema.json
{ "enum": ["pink", "blurple", false, null, ...] }

Value must be one of the specified options
JSON Schema Docs โ€” enum

Null

schema.json
{ "type": "null" }

Boolean

schema.json
{ "type": "boolean" }
โ€‰

Value of true or false
JSON Schema Docs โ€” boolean

Number

schema.json
{
"type": "number",

"type" can be either:

  • "number" Value must be a number 1.0
  • "integer" Value must be a whole number 1

JSON Schema Docs โ€” numeric

"minimum": 0,
"maximum": 100,
"exclusiveMinimum": 0,
"exclusiveMaximum": 100,

Range constraints can be defined

  • "minimum" โ‰ฅ value โ‰ฅ "maximum"
  • "exclusiveMinimum" > value > "exclusiveMaximum"

JSON Schema Docs โ€” numeric range

"multipleOf" : 10,

Multiple constraint can be defined
JSON Schema Docs โ€” numeric multiples

}

String

schema.json
{
"type": "string",
"minLength": 1,
"maxLength": 10,
โ€‰

Length constraints can be defined
JSON Schema Docs โ€” string length

"pattern": "^[a-z]+$"
โ€‰

Regex constraint can be defined
JSON Schema Docs โ€” string regex Regular Expressions

}

Array

schema.json
{
"type": "array",
โ€‰

Value of a list [...]
JSON Schema Docs โ€” array

"minItems": 1,
"maxItems": 5,

Length constraints can be defined
JSON Schema Docs โ€” array length

"uniqueItems": true,
โ€‰

Uniqueness constraint can be defined
JSON Schema Docs โ€” array uniqueness

"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

JSON Schema Docs โ€” array tuple validationยถ

"unevaluatedItems": false

Additional items constraint outside of "items", "contains" and "prefixItems" can be defined
JSON Schema Docs โ€” array unevaluated items

}

Object

schema.json
{
"type": "object",

Value of an object {...}
JSON Schema Docs โ€” object

"properties": {
"id": { "type": "string", ... },
"enabled": { "type": "boolean", ... },
"value": { "type": "number", ... },
...
โ€‰

Schema constraints can be defined
JSON Schema Docs โ€” object properties

},
"required": ["id", "enabled", ...],

Required properties constraints can be defined
JSON Schema Docs โ€” object required properties

Advanced โ€” 12 collapsed lines
"minProperties": 1,
"maxProperties": 10,
โ€‰

Length constraints can be defined
JSON Schema Docs โ€” object size

"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

schema.json
{
"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.

schema.json
{ "$comment": "...", ... }
โ€‰

JSON Schema comment
JSON Schema Docs โ€” comments

schema.json
{
"title": "...",
"description": "...",
โ€‰

Property annotations

  • "title" The Title of the schema
  • "description" The Description of the schema
"default": null,
"examples": ["zombie", 115, ...],

Value annotations

  • "default" The default value of the property
  • "examples" The example values of the property
"deprecated": true,
"readOnly": true,
"writeOnly": false

Flag annotations

  • "deprecated" Whether the property is deprecated ยท marked for removal
  • "readOnly" Whether the property is read only ยท immutable
  • "writeOnly" Whether the property is write only ยท secret
}

String Content Annotations

String Media

schema.json
{
"type": "string",
"contentMediaType": "image/png",
"contentEncoding": "base64",
โ€‰

Encoding can be one of:

  • null ยท UTF-8
  • "base64"
  • "base32"
  • "base16" ยท hexadecimal
  • "quoted-printable" ยท ASCII encoding

JSON Schema Docs โ€” Media content encoding

}

String Schema

schema.json
{
"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

schema.json
{
"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

JSON Schema Docs โ€” If then else

}

Boolean Schema composition

schema.json
{
"allOf": [
โ€‰

AND ยท all schemas must be satisfied

Example schemas โ€” 2 collapsed lines
{ "$comment": "This is a comment" },
{ "$comment": "And this is another comment" }
],
"anyOf": [
โ€‰

OR ยท at least one schema must be satisfied

Example schemas โ€” 8 collapsed lines
{
"$comment": "default suggestions",
"enum": [ "html", "svg", "math" ]
},
{
"$comment": "enum is not exhaustive, allow custom values",
"type": "string",
}
],
"oneOf": [
โ€‰

XOR ยท one schema must be satisfied and none of the rest

Example schemas โ€” 9 collapsed lines
{
"properties": {
"x": { "type": "number" },
"y": { "type": "number" },
"z": { "type": "number" }
},
"required": [ "x", "y", "z" ]
},
{ "type": "number" }
],
"not": {
โ€‰

NOT ยท this schema must not be satisfied

Example schema โ€” 3 collapsed lines
"license_plate": {
"enum": [ "NULL", "DROP", "DELETE" ]
}
}
}

Modular Schema composition

schema.json
{
"$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

JSON Schema Docs โ€” Base URI

"type": "object",
"properties": {
"location": { "$ref": "#/$defs/geolocation" },

Subschemas can be referenced by "$defs" inline definitions

"position": { "$ref": "/schemas/vector.json" },

Schemas can be referenced by relative or absolute file path

"website": { "$ref": "/schemas/types.json#/properties/url" },

Subschemas can be referenced by schema paths within a schema

"contact": { "$ref": "/schemas/types.json#email" },

Subschemas can be referenced by schema anchors within a schema

"tree": { "$ref": "#" }

Schemas can be recursively self-referenced

},
"$defs": {
โ€‰

"$defs" allows subschemas to be defined in the same file
JSON Schema Docs โ€” $defs

"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

JSON Schema Docs โ€” Bundling

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/...

JSON Schema Docs โ€” $anchor

Example schema โ€” 2 collapsed lines
"type": "string",
"pattern": "^.+@.+\\..+$"
}
}
},
...
}
}

Generic Schemas

This section is currently undocumented
Let @Stephcraft know on Discord you'd like this section Documented
Join Discord

JSON Schema Docs โ€” Dynamic references

Vocabularies

This section is currently undocumented
Let @Stephcraft know on Discord you'd like this section Documented
Join Discord

Tools

JSON Schema validator
JSON Schema store

Resources

JSON Schema
Lean JSON Schema
A Tour of JSON Schema
GitHub โ€” sourcemeta/awesome-jsonschema

This website is currently available on Desktop only
Follow @_Stephcraft to track the progress of the Mobile version #BuildInPublic

Follow