JSON Schema is a structured format used to define, validate, and enforce rules on JSON data. It provides a clear, machine-readable specification for the expected structure, data types, and constraints of a JSON object.

JSON Schema is widely used for API validation, configuration files, and data exchange formats. It ensures that JSON documents conform to a predefined structure, reducing errors in data communication.

Features

  • Validates JSON Data: Ensures that JSON data follows the expected format.
  • Supports Data Types: Defines data types like string, number, boolean, array, object, etc.
  • Flexible & Extensible: Supports complex validation rules, pattern matching, and custom constraints.
  • Human & Machine Readable: Uses JSON itself as the schema format.
  • Widely Adopted: Used in REST APIs, OpenAPI (Swagger), GraphQL, and databases.

JSON Schema Structure & Syntax

A JSON Schema is a JSON object that defines rules for validating other JSON objects.

Basic JSON Schema Example

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "minimum": 18
    },
    "email": {
      "type": "string",
      "format": "email"
    }
  },
  "required": ["name", "email"]
}
  • $schema: Defines the JSON Schema version.
  • type: Specifies the data type (object, array, string, etc.).
  • properties: Defines allowed fields and their data types.
  • required: Lists mandatory fields.
  • minimum: Sets minimum value for numeric fields.
  • format: Specifies format validation (e.g., email, date-time, uri).

Data Types & Constraints

Basic Data Types

Data TypeExample
string"hello"
integer25
number99.99
booleantrue
array["apple", "banana"]
object{"key": "value"}
nullnull

Constraints & Validation Rules

ConstraintDescriptionExample
minimum / maximumSets min/max values for numbers"minimum": 18
minLength / maxLengthRestricts string length"minLength": 3
patternRegex pattern for strings"pattern": "\^\[A-Z]"
enumLimits values to predefined options"enum":\["male", "female", "other"]
uniqueItemsEnsures array items are unique"uniqueItems": true

Example Array Validation

{
  "type": "array",
  "items": {
    "type": "string"
  },
  "minItems": 1,
  "uniqueItems": true
}
  • Ensures array contains only strings, at least one item, and no duplicates.