Generate JSON Schema from Sample JSON
What happens when you convert JSON to JSON Schema from a sample: what a generator can infer, what it gets wrong, and how to check the result before relying on it.
Schema generators take a JSON document you already have and produce a JSON Schema that describes its structure. This saves typing, especially for large or deeply nested payloads. The output is only an inference, though. It records what one example happened to contain, not what your data is allowed to contain. This review covers what the conversion does, where it falls short, and how to judge whether the result is good enough for your purpose.
Is a generated schema suitable for your intended use?
Treat a generated schema as a starting draft. The main benefit is speed: property names, nesting and basic types come out correctly, and those are the tedious parts to write by hand. The decisive tradeoff is that a generator cannot know your rules. It cannot tell which fields are optional, which values may be null, which strings follow a pattern, or which numbers have limits. If you use the draft unreviewed, it will usually be either too strict, rejecting valid data, or too loose, accepting invalid data.
- Reasonable fit: bootstrapping a schema for an existing API response or config file that you will then edit.
- Reasonable fit: quickly documenting the shape of an unfamiliar payload.
- Reasonable fit: creating a baseline to compare against later samples.
- Weak fit: enforcing validation in production without human review.
- Weak fit: defining a contract for data that varies a lot between records, if you only have one sample.
- Weak fit: cases where business rules such as ranges, enumerations or conditional fields matter more than structure.
Sometimes a schema is not the right artifact at all. If the goal is to explain a payload to people rather than to validate it, a plain-text description or an annotated example is often clearer. A short list of fields, their meaning and whether each is optional will serve a non-technical reader better than nested schema keywords.
What the conversion actually changes
Despite the word 'convert', your input is not transformed. Nothing in the sample is reordered, reversed, restyled or rewritten. The generator reads the values, works out their JSON types, and writes a separate document that uses JSON Schema keywords to describe them. The sample stays as it was. This is also different from a format conversion such as JSON to YAML, where the same data is re-expressed in another syntax. A schema contains none of your data values unless the tool copies them into keywords like examples, default or enum.
- Value types: each value is mapped to string, number or integer, boolean, null, object or array.
- Object structure: each key becomes an entry under properties, with nested objects described recursively.
- Array contents: the elements are inspected to produce an items schema.
- Required fields: depending on the tool, every key in the sample may be listed as required, or none may be.
- Optional extras: some tools also guess string formats, set additionalProperties, or add title and description placeholders. Which of these happen depends on the specific generator and its settings.
An illustrative example
The following example was written by hand to show the general mechanism. It is not the output of any particular tool, and real generators differ in their details. Here is a sample record:
{
"id": 1024,
"username": "ada",
"tags": ["admin", "beta"],
"lastLogin": null
}
A straightforward inference from that single record could look like this. A real schema would normally also declare the draft it targets with the $schema keyword, which is omitted here.
{
"type": "object",
"properties": {
"id": { "type": "integer" },
"username": { "type": "string" },
"tags": {
"type": "array",
"items": { "type": "string" }
},
"lastLogin": { "type": "null" }
},
"required": ["id", "username", "tags", "lastLogin"]
}
Compared with the input, the structure is faithful. Every key is present, the nesting matches, and each type is what the sample literally contains. The problems come from treating one record as the whole truth:
- lastLogin is typed as null only, so any record with an actual timestamp would fail validation.
- All four fields are required, even though tags or lastLogin may be absent in other records.
- Nothing stops extra, misspelled keys, because additionalProperties is not restricted.
- There is no minimum length for username, no pattern, and no limit on id.
{
"type": "object",
"properties": {
"id": { "type": "integer", "minimum": 1 },
"username": { "type": "string", "minLength": 1 },
"tags": {
"type": "array",
"items": { "type": "string" }
},
"lastLogin": {
"type": ["string", "null"],
"format": "date-time"
}
},
"required": ["id", "username"],
"additionalProperties": false
}
Every change in this revised version came from knowledge the sample does not contain: that lastLogin can hold a timestamp, that only id and username are always present, and that unknown keys should be rejected. That gap is why the review step matters.
What a single sample cannot tell a generator
- Optional versus required: a key that is present once may be missing elsewhere.
- Nullability: a null value reveals nothing about the non-null type, and a non-null value hides the fact that null is allowed.
- Enumerations: a status of "active" does not reveal the other allowed values.
- Ranges and lengths: one number or string says nothing about bounds.
- Patterns and formats: a string that looks like a date or identifier may only be inferred as a generic string.
- Empty arrays: with no elements to inspect, the items schema is either missing or left open.
- Variants: if records take different shapes depending on a type field, one sample shows only one shape.
- Where a tool accepts several samples and merges them, it can infer optional fields and type unions more accurately. Check whether your tool supports this before relying on it.
Details that commonly trip people up
- Integer versus number: since draft-06, a value such as 1.0 counts as a valid integer. A generator that types a field as integer from a sample of 3 may still reject 3.5 later, so decide which type you actually mean.
- Format is often not enforced: in drafts 2019-09 and 2020-12, format is an annotation by default, and validators do not have to reject non-matching strings unless they are configured to.
- Draft versions differ: keywords and their behavior changed between draft-04, draft-06, draft-07, 2019-09 and 2020-12. Make sure the generated draft matches what your validator supports.
- Mixed arrays: an array that holds both strings and numbers may produce a union type, an anyOf, or a loosened items schema, depending on the tool.
- Tuple-like arrays: if position matters, as in a coordinate pair, the generic items inference loses that meaning. The keywords for positional items also differ between older drafts and 2020-12.
Will the result stay readable and usable after copying or sharing?
The output is ordinary JSON text with no styling layer, so concerns specific to transformed text do not apply. Mirrored glyphs, substituted Unicode lookalikes and altered reading order are not part of this process. Copying the schema into an editor, repository or message keeps the characters intact. Indentation and key order carry no meaning in JSON, so reformatting does not change behavior. Before saving, check that the copied result parses as strict JSON. Non-ASCII property names may appear as escape sequences or as literal characters, and the two are equivalent.
Compatibility with where you use the schema matters more. Validators, code generators and API documentation tools each support particular drafts. For example, OpenAPI 3.0 uses its own adapted subset of JSON Schema, while OpenAPI 3.1 aligns with draft 2020-12. A schema that is valid for one may need adjustments for another. This review did not test specific destinations, so confirm support in your own tool's documentation.
For accessibility, a schema is read by assistive technology as plain code. Deeply nested schemas are hard to follow by ear or with a screen magnifier. Adding title and description keywords helps every reader. If the schema documents an interface for people who do not read JSON Schema, provide a plain-language field list alongside it.
Pasting real data into a generator
Samples are often copied from real API responses or exports and may contain personal or confidential values. A tool's appearance or marketing language says nothing reliable about whether input is processed locally, logged or retained. Check its documented privacy policy, or replace real values with dummy ones of the same type before pasting. The schema depends only on types and structure, so placeholder values produce the same result.
A practical workflow
- 1. Collect several representative samples, including edge cases with missing fields, nulls and empty arrays.
- 2. Replace sensitive values with placeholders of the same type.
- 3. Generate the draft, targeting the draft version your validator supports.
- 4. Review required, nullability, additionalProperties, enumerations, bounds and formats against your actual rules.
- 5. Add title and description where the meaning is not obvious.
- 6. Validate the schema against valid samples that should pass and invalid samples that should fail.
- 7. Keep the schema under version control and update it when the data contract changes.
Limits of this review
No specific generator was evaluated, and no measurements or tool comparisons are reported. The example schemas illustrate general inference behavior and were not produced by a named tool. Statements about drafts and keyword semantics reflect the published JSON Schema specifications. Behavior of particular tools, their default settings and their data handling were not verified and should be checked directly.