JSON Schema Guide

JSON Schema describes the shape of JSON data and can validate documents automatically.

Core keywords

type limits the value type. properties defines child fields. required lists mandatory keys. items describes array elements.

Example schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": {"type": "string"},
    "age": {"type": "integer", "minimum": 0}
  }
}

enum and const

enum allows a fixed set of values. const requires exactly one value.

{"role": {"enum": ["admin", "editor", "viewer"]}}

$ref and reusable schemas

$ref points to another schema definition so complex documents stay maintainable.

{"$defs":{"id":{"type":"integer"},"type":"object","properties":{"id":{"$ref":"#/$defs/id"}}}