JSON Schema Generator Tutorial with Examples

Writing a JSON Schema by hand is tedious and error-prone, especially for deeply nested API responses. A JSON Schema generator takes an example JSON payload and produces a schema automatically, which you can then use for validation, documentation, and testing. This tutorial walks through generating and refining a schema from a real example.

Starting with example JSON

Suppose you have an API endpoint that returns a user profile:

{
  "id": 1042,
  "name": "Priya Sharma",
  "email": "priya.sharma@example.com",
  "isActive": true,
  "roles": ["admin", "editor"],
  "address": {
    "city": "Bengaluru",
    "pincode": "560001"
  }
}

The generated JSON Schema

A JSON Schema generator inspects each field's type and structure and produces a Draft-07 (or newer) compliant schema like this:

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

Refining the generated schema

Auto-generated schemas are a starting point, not a final product. You typically need to add stricter validation rules manually:

  • Add "format": "email" to the email field for format-level validation.
  • Add "pattern" for the pincode field to enforce a 6-digit Indian PIN code, e.g. ^[0-9]{6}$.
  • Move optional fields out of the "required" array if they are not always present.
  • Add "enum" to the roles items if only specific role values are allowed.
  • Set "additionalProperties": false to reject unexpected extra fields.

Here is the address sub-schema after refinement:

"address": {
  "type": "object",
  "properties": {
    "city": { "type": "string", "minLength": 1 },
    "pincode": { "type": "string", "pattern": "^[0-9]{6}$" }
  },
  "required": ["city", "pincode"],
  "additionalProperties": false
}

Using the schema for validation

Once generated, the schema can be used with a validation library like Ajv in Node.js to check incoming data before it reaches your business logic:

const Ajv = require('ajv');
const ajv = new Ajv();
const validate = ajv.compile(userSchema);

const valid = validate(incomingUserData);
if (!valid) {
  console.log(validate.errors);
}

This same schema can also be reused to generate API documentation (via OpenAPI/Swagger) and to auto-generate TypeScript interfaces, avoiding duplicate work across your codebase.

Frequently Asked Questions

What is a JSON Schema generator?

A JSON Schema generator is a tool that takes an example JSON document and automatically produces a JSON Schema describing its structure, types, and required fields, saving you from writing the schema by hand.

What is JSON Schema used for?

JSON Schema is used to validate incoming JSON data against a defined structure, generate API documentation, auto-generate TypeScript or other language types, and catch malformed data before it reaches application logic.

Does a generated JSON Schema need manual editing?

Usually yes. Auto-generated schemas infer types and required fields from a single example, so you often need to manually add validation rules like minimum/maximum values, string formats, or mark fields as optional.

Try the Free JSON Schema Generator

Paste any JSON payload and instantly generate a ready-to-use JSON Schema for validation and documentation.

Related articles