🛠️DevToolKit
🧰Tools
2026-07-08·10 min read

JSON Schema Validation: A Complete Tutorial

Learn how to validate JSON data with JSON Schema. Define structure, types, constraints, and ensure data quality.

What is JSON Schema?

JSON Schema is a vocabulary that allows you to validate the structure and content of JSON data. It's like TypeScript types for JSON — but more powerful and portable across languages.

Basic Schema Example

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100
    },
    "age": {
      "type": "integer",
      "minimum": 0,
      "maximum": 150
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "tags": {
      "type": "array",
      "items": { "type": "string" },
      "uniqueItems": true
    }
  },
  "required": ["name", "email"],
  "additionalProperties": false
}

This schema validates that:

    • name is a string (1-100 chars)

    • age is an integer (0-150)

    • email is a valid email format

    • tags is an array of unique strings

    • name and email are required

    • No extra properties allowed

Common Validation Keywords

String Validation

    • minLength / maxLength — length constraints
    • pattern — regex pattern
    • format — predefined formats (email, uri, date, uuid)

Number Validation

    • minimum / maximum — value range
    • exclusiveMinimum / exclusiveMaximum — exclusive range
    • multipleOf — must be a multiple of a number

Array Validation

    • minItems / maxItems — array length
    • uniqueItems — no duplicates
    • items — schema for all items
    • prefixItems — schema for specific positions

Object Validation

    • required — list of required properties
    • properties — schema for each property
    • additionalProperties — allow/disallow extra properties
    • minProperties / maxProperties — property count

Advanced Features

Conditional Validation

{
  "if": {
    "properties": { "type": { "const": "business" } }
  },
  "then": {
    "required": ["companyName", "taxId"]
  },
  "else": {
    "required": ["firstName", "lastName"]
  }
}

Combining Schemas

    • allOf — must match ALL schemas
    • anyOf — must match at LEAST ONE schema
    • oneOf — must match EXACTLY ONE schema
    • not — must NOT match the schema

Reusable Definitions

{
  "$defs": {
    "address": {
      "type": "object",
      "properties": {
        "street": { "type": "string" },
        "city": { "type": "string" }
      }
    }
  },
  "properties": {
    "home": { "$ref": "#/$defs/address" },
    "work": { "$ref": "#/$defs/address" }
  }
}

Using JSON Schema in Code

JavaScript/TypeScript (Ajv)

import Ajv from "ajv";
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(data);
if (!valid) console.log(validate.errors);

Python (jsonschema)

import jsonschema
jsonschema.validate(instance=data, schema=schema)

Conclusion

JSON Schema is the standard way to validate JSON data. It's used in APIs, configuration files, and data pipelines. Start with a simple schema and add complexity as needed.

Use our JSON Formatter to format your JSON before validating it against your schema.