Draft 2020-12 · AJV compatible · spec-correct

Generate JSON Schema from a sample

Paste any JSON object and get a valid JSON Schema with inferred types, required fields, and nesting — ready for AJV, form generators, or API docs.

JSON Schema Generator Workspace
Input size
132 B
7 lines
Schema size
671 B
7 keys
Draft
2020-12
current spec
Status
Valid
JSON Schema
Draft
input · json sample
output · json schema
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "active": {
      "type": "boolean"
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "address": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string"
        },
        "country": {
          "type": "string"
        }
      },
      "required": [
        "city",
        "country"
      ]
    }
  },
  "required": [
    "id",
    "name",
    "active",
    "tags",
    "address"
  ]
}
Step-by-step Tutorial

How to generate a JSON Schema from a JSON sample

JSON Schema is the lingua franca of JSON validation, form generation, and API documentation. This generator infers a schema from a representative sample so you don't hand-author every nested shape.

  1. 01

    Paste a representative JSON sample

    Use a real-world example — API response, config object, or analytics event. The generator infers types and required fields from the sample.

    Both pretty-printed and minified JSON work.

    Inputs parse through the native JSON.parse reviver chain — strict RFC 8259. Any syntax error returns line and column for the exact offending byte.

    sampleAPI responseRFC 8259JSON.parsestrict
    input · json · raw.json · RFC 8259
    {"id":1,"name":"Ada"}
    loaded · parsed RFC 8259RFC 8259 · ready
  2. 02

    Choose Draft 2020-12 or draft-07

    2020-12 is the current spec (most modern features, used by latest AJV and most tooling).

    draft-07 is the most widely supported — pick this for older Pydantic, AJV 6, or legacy form generators.

    2020-12draft-07$schema
    draft$schema
    {
      "$schema": "https://json-schema.org/draft/2020-12/schema",
      "type": "object"
    }
    spec-correctready
  3. 03

    Review the inferred schema

    The generator emits type annotations (string, number, integer, boolean, null, array, object), properties for nested fields, and required lists all keys present in the sample.

    Mixed-type arrays merge into union types. Integer detection: 1 → integer, 1.5 → number.

    typepropertiesrequired
    output · schema.json
    {
      "type": "object",
      "properties": {
        "id": {"type": "integer"},
        "name": {"type": "string"}
      },
      "required": ["id", "name"]
    }
    validatedspec-correct
  4. 04

    Use with AJV, forms, or docs

    Pipe the schema to AJV for runtime validation, react-jsonschema-form to auto-generate UIs, or OpenAPI / Swagger to document your endpoints.

    AJVrjsfOpenAPI
    usageAJV
    const Ajv = require("ajv");
    const ajv = new Ajv();
    const validate = ajv.compile(schema);
    if (!validate(data)) console.log(validate.errors);
    spec-drivenvalidated
Pro tips

Production-grade JSON Schema generation

Add constraints by hand

The generator emits types but not constraints — add minLength, format: "email", pattern, etc. for real validation.

Adjust required carefully

All fields in the sample become required. Trim manually if some are optional in production.

Mixed-type arrays

The generator merges types. Verify the resulting union matches your actual data contract.

Use $defs for reuse

For repeated shapes (Address, User), extract to $defs and reference with $ref.

Add format hints

Strings can use format: "email", "uri", "date-time", "uuid" for richer validation.

OpenAPI subset

OpenAPI 3.1 aligns with JSON Schema 2020-12. Earlier OpenAPI versions need slight tweaks.

Use Cases

When to reach for JSON Schema

Wherever you need machine-readable contracts for JSON data.

Runtime validation with AJV

Industry-standard validator. Plug the schema in and reject bad input at the edge. Output round-trips losslessly through JSON.parse + JSON.stringify, so the file you save is byte-identical to what you see. Streaming parsers, jq pipelines, and Postgres jsonb columns all accept the result without complaint.

10× faster to scan

Auto-generated forms

react-jsonschema-form, JSON Forms, and Uniforms all render forms from a schema. Skip hand-built UIs. Pairs perfectly with sort-keys + diff for review-friendly fixtures in CI. Whitespace-only churn disappears and reviewers see real changes, which keeps PR comments focused on logic rather than indentation.

form codegen

OpenAPI / Swagger docs

OpenAPI 3.1 components.schemas accept JSON Schema directly. One artifact for docs and runtime validation. Handles emoji and non-ASCII via UTF-8 throughout — your data survives copy-paste between editors, terminals, and IDEs. Combining marks and ZWJ sequences come out the other side intact.

OpenAPI ready

Contract tests

Validate that mock data and real responses both conform to the same schema. Catches drift early. Re-parses on every keystroke so syntax errors are caught at the exact character. Strict RFC 8259 — no trailing commas, no comments, no unquoted keys — keeps downstream parsers happy.

round-trip safe

LLM tool definitions

OpenAI / Anthropic tool calls accept JSON Schema. Generate from a sample for instant tool definitions. Streams large documents without a server round-trip — privacy stays in the tab. Heavy parsing runs through useMemo so even 5 MB payloads keep the editor responsive.

tool-call native

Database column constraints

Postgres jsonb columns support JSON Schema validation. Constrain shape at the storage layer. Output is byte-stable: same input plus same options always produces identical bytes, ideal for content-addressed caches and snapshot tests. Downloads ship with the application/json MIME so editors apply syntax highlighting.

DB-level safety
FAQ

Frequently asked questions

About JSON Schema generation.

Choose between Draft 2020-12 (the current spec) and draft-07 (the most widely-supported legacy version). Both are compatible with AJV.
Yes — by default. The generator assumes every key in the sample is required because it can't infer absence from a single sample. Trim manually for optional fields.
Item schemas are merged. Single-type arrays produce a single item schema; mixed-type arrays produce a union (type: [...]).
Yes — 1 → integer, 1.5 → number. JSON Schema treats these distinctly for validation.
No. Inference runs locally — open DevTools → Network to confirm.
Suggest a tool

Something missing?

Tell us what you need. We ship the top requests every two weeks.

Sent straight to the maintainers · no account needed
47 in queue·8 shipped this month
Request queued. Thanks!