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:
- Validation at the boundary. Reject a bad request with a precise error ("age must be an integer") instead of a 500 three layers deeper.
- Editor support. VS Code validates and autocompletes JSON and YAML files against a schema — that is how it knows the keys of package.json.
- Contract tests. Validate real API responses against the schema in CI and catch breaking changes before clients do.
- Typed clients and structured AI output. Code generators turn schemas into TypeScript types; LLM APIs accept a JSON Schema to force well-formed output.
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:
- Scalars map to their JSON Schema types, with whole numbers becoming
integer(stricter and almost always what an API means) and decimals becomingnumber. - Objects become
type: "object"with apropertiesmap, and the keys present in the sample are listed asrequired. - Arrays are the interesting case: the generator infers a schema for every element and merges them — properties are unioned, and only keys present in every element stay required. Genuinely mixed arrays become an
anyOf. - Recognizable strings get a
format: ISO dates becomedate-time, and emails, URLs, UUIDs and IPv4 addresses are tagged too — validators and docs both benefit.
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:
- Optional keys. If a field can be absent but your sample happened to include it, delete it from
required. (Feed the generator an array of several samples and it does this intersection for you.) - Nullability. A field that was
nullin the sample is typednull; widen it to what it really is, e.g.{"type": ["string", "null"]}. - Constraints. Add
minimum,maxLength,patternandenumwhere business rules exist — no sample can reveal that status is one of three values. - Descriptions. A one-line
descriptionper property is what turns a schema into documentation humans read. - additionalProperties. Set it to
falseon strict API contracts so typos in keys fail loudly instead of passing silently.
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 — freeFrequently 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
- How to review code with AI, free and without sign-up
- How to convert code between languages with AI (Python, Java, JavaScript and more)
- How to generate unit tests with AI: from zero tests to a real suite
- The best free code review tools in 2026
- AI code tools for beginners: learn faster without learning less
- 10 free AI code tools every developer needs in 2026
- DoAide Code: the free alternative to GitHub Copilot
- How to review code with AI for free: workflows that stick
- How to write clean code in 2026
- Best practices for code documentation
- The best free online code formatter in 2026
- Online regex tester and builder: write regular expressions that actually work
- Free online SQL formatter and validator
- Code snippet sharing for developers: stop pasting code into chat
- The flexbox cheat sheet: every property and what it actually moves
- Meta tags for SEO: what your pages actually need in 2026
- UUID v4 vs v7: which one should you use?
- CSS Grid explained: tracks, fr, minmax and template areas