JSON Schema lets you define the exact structure, types, and constraints of your JSON data — then validate any JSON against it. It's the standard for API documentation, form validation, and data integrity checks.

What is JSON Schema?

JSON Schema is itself a JSON document that describes the shape of other JSON data. Think of it as a blueprint that says "a valid user object must have a name string, an integer age between 0 and 150, and an email that matches an email pattern."

A basic JSON Schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "User",
  "description": "A registered user",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "description": "Unique user identifier"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 100
    },
    "age": {
      "type": "integer",
      "minimum": 0,
      "maximum": 150
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "role": {
      "type": "string",
      "enum": ["admin", "user", "moderator"]
    }
  },
  "required": ["id", "name", "email"]
}

Type keywords

{ "type": "string" }   // "hello"
{ "type": "number" }   // 3.14 or 42
{ "type": "integer" }  // 42 (not 3.14)
{ "type": "boolean" }  // true or false
{ "type": "null" }     // null
{ "type": "array" }    // [1, 2, 3]
{ "type": "object" }   // {"key": "value"}

// Multiple types allowed:
{ "type": ["string", "null"] }  // string or null

String constraints

{
  "type": "string",
  "minLength": 3,
  "maxLength": 50,
  "pattern": "^[a-zA-Z0-9_]+$"  // alphanumeric + underscore only
}

// Built-in formats (validation depends on the library)
{ "type": "string", "format": "email" }
{ "type": "string", "format": "date" }       // "2023-11-14"
{ "type": "string", "format": "date-time" }  // "2023-11-14T22:13:20Z"
{ "type": "string", "format": "uri" }

Array validation

{
  "type": "array",
  "items": { "type": "string" },   // all items must be strings
  "minItems": 1,
  "maxItems": 10,
  "uniqueItems": true              // no duplicates
}

// Tuple validation (each position has its own schema)
{
  "type": "array",
  "prefixItems": [
    { "type": "string" },   // first item: string
    { "type": "number" },   // second item: number
    { "type": "boolean" }   // third item: boolean
  ]
}

Validate JSON Schema in JavaScript with AJV

import Ajv from 'ajv';
import addFormats from 'ajv-formats';

const ajv = new Ajv();
addFormats(ajv); // adds email, date, uri validation

const schema = {
  type: 'object',
  properties: {
    name: { type: 'string', minLength: 1 },
    age: { type: 'integer', minimum: 0 },
    email: { type: 'string', format: 'email' }
  },
  required: ['name', 'email']
};

const validate = ajv.compile(schema);

// Valid
console.log(validate({ name: 'Alice', email: 'alice@example.com', age: 28 }));
// true

// Invalid
console.log(validate({ name: 'Alice' })); // false — missing email
console.log(validate.errors);
// [{ message: "must have required property 'email'" }]

Validate in Python with jsonschema

from jsonschema import validate, ValidationError

schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0}
    },
    "required": ["name"]
}

# Valid
validate(instance={"name": "Alice", "age": 28}, schema=schema) # no error

# Invalid
try:
    validate(instance={"age": 28}, schema=schema)  # missing name
except ValidationError as e:
    print(e.message)  # "'name' is a required property"

Try it free — JSON Formatter & Validator

Validate JSON syntax instantly before testing against your schema.

Open tool →