JSON Schema: inferring one from your data
25 NOVEMBER 2026 · 8 MIN READ
Nobody writes a schema for a payload they have never seen. Inference gets you the shape, the types and the fields every record agreed on — and nothing else. Knowing which half you still have to write is the difference between a schema that documents and one that protects.
A schema and a sample of data are two views of the same decision, and the practical order is almost always sample first. Inference reads a value and emits a draft 2020-12 document describing it. It is a genuinely useful starting point and a genuinely dangerous finished product, because it can only report what the records in front of it contain.
{ "id": 1, "email": "a@b.co", "tags": ["x"] }
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "GeneratedSchema",
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string", "format": "email" },
"tags": { "type": "array", "items": { "type": "string" } }
},
"required": ["id", "email", "tags"]
} How each keyword is decided
| Keyword | Where it comes from |
|---|---|
type | the observed JSON type; when records disagree, a list of types rather than a guess |
properties | the union of keys seen across the sample |
required | the intersection: only keys that appeared in every record |
items | for an array root, the merged shape of the elements — not a per-position tuple |
format | a small set of recognised string shapes, including email |
title | always GeneratedSchema, which is the first thing you should change |
[ { "a": 1, "b": "x" }, { "a": "2", "c": true }, { "a": 3 } ]
items -> { "type": "object",
"properties": {
"a": { "type": ["integer", "string"] },
"b": { "type": "string" },
"c": { "type": "boolean" }
},
"required": ["a"] } That union is the most valuable line in the output. a is sometimes a number and sometimes a string, which is either a bug in the producer or a field that was never a number — and both answers are worth having before the data reaches a warehouse. Notice also that b and c exist but are optional: one record out of three is enough to knock a key out of required.
What a sample cannot tell you
- ▸Which optional fields are actually mandatory.
requiredrecords what you sampled, not what the contract says. Ten records that all happen to includeemailproduce a schema that permits its absence — and if the sample is one record, everything looks required. - ▸Ranges and lengths. No generator will turn
retentionDays: 30intominimum: 1, or a two-letter country intomaxLength: 2. - ▸Patterns. An identifier that always looks like
usr_<9 digits>is still just a string until you write thepattern. - ▸Enums. A status field with three values in the sample does not mean three values in the domain; inference cannot know whether a fourth is legal.
- ▸Whether extra properties are allowed. Nothing is emitted for
additionalProperties, so unknown keys pass — the opposite of what most people want from a boundary schema. - ▸Which type disagreement is intentional. A union is a fact about the sample; deciding whether to widen the schema or fix the producer is still yours.
Numbers deserve their own caveat
A sample of [1, 2.5] infers ["integer", "number"], which is technically honest and practically noise: every integer is already a number, so the union says only “number”. Simplify it by hand. More importantly, inference runs on a parsed value, so any identifier longer than 17 digits has already been rounded before a schema ever sees it — the generated "type": "integer" then documents a corruption instead of preventing one. The number precision guide covers that failure; the schema-side fix is to declare such fields string even when the sample says otherwise.
Drafts, and why validation depends on them
The emitted document names draft 2020-12, and the validator picks the engine build that matches the $schema it is given: 2020-12 and 2019-09 documents load their own dialect, anything else falls back to draft-07. That matters when you copy snippets from older material, because the two revisions renamed things: definitions became $defs, an array-form items became prefixItems, and exclusiveMinimum changed from a boolean modifier into a number. Paste a draft-07 idiom into a 2020-12 document and it will not mean what you expect — or, with strict mode off, will simply be ignored.
The loop that makes it useful
- 01 Collect a sample wide enough to be boring: every producer version, every optional field you know exists, at least one record per code path. If the source is a log or an export in lines, run it through NDJSON to JSON first so inference sees the set rather than one element.
- 02 Infer, then validate the sample against its own schema. It should pass; if it does not, the generator and your expectations disagree, and that disagreement is the finding.
- 03 Validate a deliberately bad record — a missing field, a wrong type — and read the error list. Failures come back as location plus message, something like
/ must have required property 'email'and/id must be integer, and every violation is reported rather than only the first. - 04 Add the constraints inference cannot know:
requiredentries the contract demands,pattern,enum,minimum, andadditionalProperties: falseon objects whose shape is closed. - 05 Give it a real
titleand an$id, and split reusable pieces into$defsso the address and the money object are defined once. - 06 Keep the file next to the API and regenerate on release. Diffing an old schema against a new one is the cheapest contract check there is: a widened
typeor a droppedrequiredentry is a breaking change that nobody approved. - 07 When the consumers are typed, generate from the same sample with JSON to TypeScript as well — two projections of one dataset, and any mismatch between them is a documentation bug you found early.
NOTE An inferred schema is documentation that can run, not a specification. It records what your sample did; it takes a human to write down what is forbidden. The useful split is mechanical on one side — types, properties, array membership — and deliberate on the other: required fields, bounds, patterns and the decision about unknown keys.