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.

One record in, one schema out
{ "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

KeywordWhere it comes from
typethe observed JSON type; when records disagree, a list of types rather than a guess
propertiesthe union of keys seen across the sample
requiredthe intersection: only keys that appeared in every record
itemsfor an array root, the merged shape of the elements — not a per-position tuple
formata small set of recognised string shapes, including email
titlealways GeneratedSchema, which is the first thing you should change
Records that disagree produce a union
[ { "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. required records what you sampled, not what the contract says. Ten records that all happen to include email produce a schema that permits its absence — and if the sample is one record, everything looks required.
  • ▸Ranges and lengths. No generator will turn retentionDays: 30 into minimum: 1, or a two-letter country into maxLength: 2.
  • ▸Patterns. An identifier that always looks like usr_<9 digits> is still just a string until you write the pattern.
  • ▸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

  1. 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.
  2. 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.
  3. 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.
  4. 04 Add the constraints inference cannot know: required entries the contract demands, pattern, enum, minimum, and additionalProperties: false on objects whose shape is closed.
  5. 05 Give it a real title and an $id, and split reusable pieces into $defs so the address and the money object are defined once.
  6. 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 type or a dropped required entry is a breaking change that nobody approved.
  7. 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.
The schema module listing three validation errors at once for a record that no longer matches the schema inferred from it: a missing required property qty, an id that must be an integer and tags that must be an array
Every failing path in one run, not only the first

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.

TOOL MATRIX

Tools for this guide

Keyboard shortcuts
FORMATTING
Format input Ctrl/⌘ + Enter
Toggle auto-format Ctrl/⌘ + Shift + A
CLIPBOARD & FILES
Copy output Ctrl/⌘ + K
Open JSON file Ctrl/⌘ + O
Download output Ctrl/⌘ + S
Save snippet Ctrl/⌘ + Shift + N
MODULES
Open find & replace Ctrl/⌘ + Shift + F
Lock / unlock output Ctrl/⌘ + Shift + L
Show this dialog Ctrl/⌘ + /
Local history
Save snippet
Erase local data

Nothing is stored in this browser yet.

Removes the saved draft, history, snippets and formatting options for this site. Documents you typed stay in the editor until the page reloads.