Skip to content
uencoder

JSON Schema

JSON Schema is a vocabulary for describing the structure of JSON documents, defining types, constraints, and validation rules; it is the schema standard for JSON.

Purpose

To give JSON a validation mechanism similar to XSD, ensuring data matches the expected structure.

Mechanism

A schema describes itself in JSON: type declares the type, properties declares object properties, items declares array elements, required declares mandatory fields, and minimum/maximum/pattern declare constraints. Draft 2020-12 is the latest version. Validators (ajv) check documents against it.

Use cases

  • API request/response validation
  • Configuration file checking
  • Form data validation
  • OpenAPI type definitions

Examples

  • Input
    {"type":"object","properties":{"age":{"type":"integer","minimum":0}}}
    Output
    Validates that age is a non-negative integer

    Constraint declaration

Common errors

Using an outdated draft version breaks validator compatibility
Pin the draft version and use a matching validator

Related terms

Related tools

References