JSON Schema Validation

Practical JSON Schema examples for required fields, types, enums, arrays, nested objects, and validation workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Schema basics
Basic object schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "active": { "type": "boolean" }
  },
  "required": ["name", "active"],
  "additionalProperties": false
}

# Require a string name and boolean active flag.

Enum restriction
{
  "type": "object",
  "properties": {
    "status": {
      "type": "string",
      "enum": ["draft", "published", "archived"]
    }
  },
  "required": ["status"]
}

# Restrict a value to a known set of strings.

Array item validation
{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": { "type": "integer" },
      "email": { "type": "string", "format": "email" }
    },
    "required": ["id", "email"]
  }
}

# Validate every item in an array.

## Schema validation workflows
Validate with Ajv CLI
npx ajv-cli validate -s schema.json -d data.json

# Validate a JSON file against a schema file.

Validate multiple files with Ajv
npx ajv-cli validate -s schema.json -d 'fixtures/*.json'

# Validate a whole set of JSON documents.

Recommended next

No recommendations yet.