DevTools Logo

JSON Schema Cheat Sheet

Quick reference for JSON Schema: core keywords, validation rules, arrays, nested objects, references, and common patterns.

Data Formats
json
json-schema
validation

JSON Schema describes the shape of JSON data and validates whether a document matches. Draft 2020-12 is the current standard; many tools still default to draft-07. A schema is itself valid JSON.

Core Keywords

Table
KeywordPurposeExample
$schemaWhich draft the schema follows"$schema": "https://json-schema.org/draft/2020-12/schema"
$idCanonical URI identifier for the schema"$id": "https://example.com/pet.schema.json"
$refReference another schema or subschema"$ref": "#/definitions/name"
typeExpected type(s)"type": "string" or ["string", "null"]
title / descriptionDocumentation"description": "The pet's name"
defaultDefault value (informational)"default": "Fido"

Type-Specific Keywords

Table
TypeKeywords
stringminLength, maxLength, pattern, format
number / integerminimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf
arrayitems, prefixItems, minItems, maxItems, uniqueItems, contains
objectproperties, required, additionalProperties, minProperties, maxProperties, propertyNames, patternProperties

A Validation Example

json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/order.schema.json",
  "type": "object",
  "required": ["id", "customer", "items"],
  "properties": {
    "id": { "type": "string", "minLength": 1 },
    "customer": {
      "type": "object",
      "required": ["email"],
      "properties": {
        "email": { "type": "string", "format": "email" },
        "name": { "type": "string" }
      },
      "additionalProperties": false
    },
    "items": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": ["sku", "qty"],
        "properties": {
          "sku": { "type": "string" },
          "qty": {
            "type": "integer",
            "minimum": 1,
            "exclusiveMaximum": 1000
          }
        }
      }
    }
  }
}

Arrays: items vs prefixItems

items validates every element against one schema. prefixItems (draft 2020-12; called items as an array in draft-07) validates positional tuples.

json
{
  "type": "array",
  "prefixItems": [
    { "type": "string" },
    { "type": "integer" }
  ],
  "items": false,
  "minItems": 2
}

Combining Keywords

Table
KeywordBehaviorExample
allOfEvery subschema must pass"allOf": [{ "type": "object" }, { "required": ["id"] }]
anyOfAt least one subschema passes"anyOf": [{ "type": "string" }, { "type": "number" }]
oneOfExactly one subschema passes"oneOf": [{ "required": ["car"] }, { "required": ["boat"] }]
notThe subschema must fail"not": { "type": "null" }

$ref and Definitions

$ref keeps schemas DRY. Relative refs resolve against $id when one is set.

json
{
  "$defs": {
    "address": {
      "type": "object",
      "required": ["city"],
      "properties": {
        "city": { "type": "string" },
        "zip": { "type": "string", "pattern": "^[0-9]{5}$" }
      }
    }
  },
  "type": "object",
  "properties": {
    "billing": { "$ref": "#/$defs/address" },
    "shipping": { "$ref": "#/$defs/address" }
  }
}

Common Pitfalls

Table
PitfallFix
Forgetting additionalProperties: falseAllows typos to pass silently; set it where strictness matters.
exclusiveMinimum: 1 at top level (draft-07)Draft-07 used a boolean form; draft-06+ uses the numeric form shown above.
Using required inside propertiesrequired is a sibling of properties, not a child.
format not enforced by defaultformat is annotation-only unless the validator opts in; test with a strict validator.
$ref into a non-existent pathReference errors fail the whole schema; validate the schema first.

References