DoAide Code

Guides · Updated 2026-10-23

How to generate a JSON Schema from a JSON sample

Every API contract argument ends the same way: someone finally writes a schema. JSON Schema is how services tell each other — and tell their tests, their editors and their AI tools — exactly what a payload looks like: which keys exist, which are required, what type each one is. OpenAPI request bodies, form validators, config file linting in VS Code, structured output from LLMs — all of it runs on JSON Schema.

Writing one by hand is tedious, and the fastest way to start is to generate it from a real sample of your data. This guide shows how schema inference works, what it gets right automatically, and the handful of things you should still edit by hand. You can follow along with the free JSON Schema generator — paste JSON, get a draft 2020-12 schema instantly, nothing uploaded.

What a JSON Schema actually buys you

A schema is machine-checkable documentation. Concretely it gives you:

The draft to use in 2026 is 2020-12 — it is what OpenAPI 3.1 and modern validators (Ajv, jsonschema) speak natively.

How generating a schema from a sample works

A generator walks your sample value and mirrors its shape:

Paste a realistic sample into the generator and read the output next to this list — each rule is visible in the result.

What to edit by hand afterwards

Inference sees one sample, not your intent. After generating, spend five minutes on:

A minimal validation workflow

With a schema in hand, wiring validation takes minutes. In Node with Ajv:

import Ajv from 'ajv/dist/2020.js'
import addFormats from 'ajv-formats'

const ajv = new Ajv()
addFormats(ajv)
const validate = ajv.compile(schema)

if (!validate(payload)) {
  console.log(validate.errors) // instancePath, message, params
}

In Python, jsonschema.validate(payload, schema) does the same. Put it at every boundary you do not control: request bodies, webhook payloads, files users upload, responses from third-party APIs. Validate your own config files in the editor by adding a $schema key pointing at your schema file.

Working with the JSON itself first? The JSON formatter validates syntax and shows the structure as a tree, and the JSON path finder gives you the exact path to any value.

Generate a JSON Schema from your JSON — free

Frequently asked questions

Can I generate a JSON Schema from a JSON file for free?

Yes — paste the JSON into the free generator at code.doaide.com/tools/json-schema-generator and copy the draft 2020-12 schema. It runs entirely in your browser with no sign-up, and your data is never uploaded.

Which JSON Schema draft should I use in 2026?

Draft 2020-12. It is the current standard, it is what OpenAPI 3.1 uses natively, and every maintained validator (Ajv, python-jsonschema, everit) supports it. Only pick draft-07 when a legacy tool forces it.

Does a generated schema replace writing one by hand?

It replaces the boring 80%: structure, types, required lists and formats. You still add intent by hand — optional keys, enums, ranges and descriptions — which the guide covers section by section.

What is the difference between integer and number in JSON Schema?

number accepts any JSON number including decimals; integer accepts only whole values. Generators emit integer when the sample value is whole, which is stricter and usually correct for ids and counts.

More guides