One record is not a contract
Here are two responses from GET /api/users. The second omits avatarUrl, has a null last-seen time and a fractional score — three ordinary differences that a single-sample generator would get wrong:
[
{ "id": 1042, "email": "ada@example.com", "displayName": "Ada",
"avatarUrl": "https://cdn.example.com/ada.png", "roles": ["admin", "editor"],
"lastSeenAt": "2026-11-01T09:30:00Z", "score": 12 },
{ "id": 1043, "email": "alan@example.com", "displayName": "Alan",
"roles": ["viewer"], "lastSeenAt": null, "score": 9.5 }
]Fed to the JSON Schema generator, that sample produces this. Read it as a list of decisions the data made on your behalf:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "User",
"type": "array",
"items": {
"$ref": "#/$defs/UserItem"
},
"$defs": {
"UserItem": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string" },
"displayName": { "type": "string" },
"avatarUrl": { "type": "string" },
"roles": { "type": "array", "items": { "type": "string" } },
"lastSeenAt": { "type": ["string", "null"] },
"score": { "type": "number" }
},
"required": [
"id", "email", "displayName", "roles", "lastSeenAt", "score"
],
"additionalProperties": false
}
}
}Three lines are worth stopping at. avatarUrl is described but not required, because one record lacked it. lastSeenAt is a union of string and null, because a null actually appeared. And score is number, not integer, because 9.5 ruled the narrower type out.
Required means observed, not promised
The most useful property of a schema generator is also the most misunderstood: required is computed from sample count, not guessed from field names. Paste a single record and every key is required — the tool saw no evidence that any could be absent. Here is the same generator with only the first record:
"avatarUrl": { "type": "string" }, // present in properties
"lastSeenAt": { "type": "string" }, // no null seen
"score": { "type": "integer" }, // no fraction seen
"required": ["id", "email", "displayName", "avatarUrl",
"roles", "lastSeenAt", "score"]All three differences point the same way. avatarUrl becomes required, lastSeenAt loses its null arm and score narrows to integer. Each edit makes the schema stricter, and each one would reject a response you already know the endpoint can send. Ten mixed records are worth more than one perfect-looking one.
Six decisions the generator makes for you
Every JSON-to-schema tool answers the same six questions. The output looks different mainly because they answer differently, so these are worth knowing before you trust a result:
| The sample shows | The schema writes | What it means |
|---|---|---|
| A key is missing from one record | Left out of required | Correct for that sample. If the key is actually always sent, you under-constrained — add it back by hand. |
| Whole numbers, then one fraction | integer widens to number | integer would reject the fractional record. Widening keeps the schema valid; narrowing by hand is a real decision. |
| null appears alongside a type | type: ["string", "null"] | null is a value, not an absent key. The union records exactly what the sample contained. |
| Object or primitive in the same field | anyOf | A primitive-only union fits in a type array; the moment an object joins, the schema needs anyOf. |
| A nested object shape | $defs + $ref | Repeated shapes are named and referenced, so a change to the shape is made once. |
| A key no record showed | additionalProperties: false | Strict by default: unknown keys fail validation. Turn it off for responses that gain fields over time. |
Widening is the rule that keeps a generated schema usable: an integer becomes number at the first fraction, and a primitive union becomes a compact type array. A union that mixes an object with a primitive cannot use that shorthand, so it becomes anyOf.
Four things the sample cannot tell you
A perfect generator would still stop at four questions. The data does not contain the answers; it is information about your domain, not about this payload.
- Which keys are truly required. The generator knows which keys were present, not which ones the contract promises. A key in every record of a small sample may still be optional in production.
- Which numbers are money. A price of
129.5is just a number. Add amultipleOfrule, or store minor units as integers — the sample cannot tell you which you need. - Which strings have a format. An ISO timestamp, an email and a UUID are all
stringto a generator.format: date-timeandpatternare yours to add. - Which fields are enums. A status field showing
"shipped"becomesstring, not a one-value enum. That is the only honest reading of a single observation.
That is why a generated schema is a correct skeleton rather than a finished contract. The mechanical work — required keys, type unions, nested definitions — is done. The four edits above are yours.
Keeping the schema honest after generation
- Generate from real payloads, not from documentation. Docs describe the intended contract; responses describe what the endpoint returns today. When they disagree, the schema that validates traffic is the one built from traffic.
- Name the root before you copy. The title becomes the root and prefixes nested definitions —
UserItemreads better in an editor thanRootItem. - Decide strictness per endpoint.
additionalProperties: falseon a stable internal contract; leave it open on a third-party response that adds fields without notice. - Commit the file and let it diff. When the API changes, a regenerated schema shows the change in review. One that is rebuilt invisibly at runtime gives you no such signal.
One trap sits upstream of all of this: if the sample contains a very large integer, JavaScript has already lost its last digits by the time the schema is generated. The number precision guide explains what to check first. For the compile-time half of the same problem, see JSON to TypeScript: your sample is not a schema.