Skip to content
data2026-07-252 min read

What Is JSON Schema

JSON Schema is a standard for describing and validating JSON data structures. It defines the properties, types, constraints, and defaults of JSON objects.

Example JSON Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "age": { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" },
    "tags": { "type": "array", "items": { "type": "string" } }
  },
  "required": ["name", "email"]
}

Why You Need JSON Schema

  1. API documentation: Auto-generate API request/response structure docs
  2. Data validation: Validate JSON data correctness at runtime
  3. Form generation: Auto-generate form UI from Schema
  4. Mock data: Generate test data based on Schema
  5. Type safety: Generate TypeScript type definitions from Schema
  6. Config validation: Verify config file format is correct

JSON Schema Core Keywords

| Keyword | Description | Example | |---------|-------------|---------| | type | Data type | "string", "number", "boolean", "array", "object" | | properties | Object properties | { "name": { "type": "string" } } | | required | Required fields | ["name", "email"] | | minimum/maximum | Number range | "minimum": 0, "maximum": 100 | | minLength/maxLength | String length | "minLength": 1, "maxLength": 255 | | enum | Enum values | ["active", "inactive", "pending"] | | format | Format validation | "email", "uri", "date-time" | | items | Array element type | { "type": "string" } |

How to Use an Online Tool

Using DevToolkit Pro's JSON Schema Generator:

  1. Paste a JSON object example
  2. The tool auto-analyzes the data structure
  3. Generates complete JSON Schema
  4. Supports custom constraints (required, ranges, etc.)
  5. One-click copy Schema code

Auto-Generate from JSON Example

Input

{
  "id": 1,
  "name": "Alice",
  "email": "alice@example.com",
  "isActive": true,
  "tags": ["developer", "admin"],
  "address": { "city": "Shanghai", "zip": "200000" }
}

Output Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "id": { "type": "integer" },
    "name": { "type": "string" },
    "email": { "type": "string" },
    "isActive": { "type": "boolean" },
    "tags": { "type": "array", "items": { "type": "string" } },
    "address": {
      "type": "object",
      "properties": {
        "city": { "type": "string" },
        "zip": { "type": "string" }
      }
    }
  }
}

JSON Schema Validation in Code

JavaScript (Ajv)

const Ajv = require('ajv');
const ajv = new Ajv();

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

const validate = ajv.compile(schema);
const valid = validate({ name: 'Alice', age: 28 });
console.log(valid); // true

Python

from jsonschema import validate, ValidationError

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

try:
    validate(instance={"name": "Alice", "age": 28}, schema=schema)
    print("Validation passed")
except ValidationError as e:
    print(f"Validation failed: {e.message}")

FAQ

What JSON Schema Versions Are Supported?

Draft 7 (2018-02) and 2020-12 are most commonly used. Draft 7 has the best compatibility — recommend using it first. Most validation libraries support Draft 7.

Can Schemas and TypeScript Interfaces Be Converted?

Yes. Use typescript-json-schema to generate Schema from TS, or json-schema-to-typescript to generate TS interfaces from Schema. Tools automate this process.

Can JSON Schema Validate Nested Objects?

Yes. JSON Schema natively supports nested object definitions using recursive properties. The $ref keyword references defined sub-schemas to avoid duplication.


This article is brought to you by DevToolkit Pro. More developer tools at the homepage.


ad