How to generate a JSON Schema from JSON
A JSON Schema describes the shape of JSON: which keys exist, what type each value has and which keys are required. To generate a JSON Schema from JSON, the tool reads your sample, records the type of every value and writes the matching keywords. An object becomes type object with properties, a list becomes type array with items, and text, numbers and booleans get their own types.
A schema made from one sample is only a first draft, because one sample cannot show which fields are optional. Paste several samples and the json schema generator merges them. The processing happens in your browser, as the Privacy Policy describes.
{"id":1,"email":"ana@example.com","tags":["a"]}
{"id":2,"email":"ben@example.com","phone":null}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": { "type": "integer" },
"email": { "type": "string", "format": "email" },
"tags": { "type": "array", "items": { "type": "string" } },
"phone": { "type": "null" }
},
"required": ["id", "email"]
}Why some fields are not required
With the default setting, a property is required only when it appears in every object that was seen at that place. In the example above, id and email are in both samples and tags and phone are in one each, so only id and email are required. That is the main reason to paste more than one sample.
- In every sample: required means present in all objects seen. This is the default and the safest.
- Any sample: every property that appeared anywhere is required. Use it when your samples are all complete.
- None: the required list is left out, so any object that has the right types passes.
A single sample makes every property look required, so the tool adds a reminder when you paste only one.
Arrays and mixed types
The items of all arrays at the same place are merged into one items schema. If some items have an extra key, that key is optional in the merged schema, and if one item is a number and another a string, the type becomes a list of both. An array that was empty in every sample has no items schema, and the notes name it so you can add a sample that has items.
When a value has more than one type, for example text in one sample and null in another, the default is a type list such as type: [string, null]. Choose anyOf to get one branch for each type, which some OpenAPI tools prefer. Whole numbers and decimals merge into number, and a field that is only ever whole numbers is integer.
By default every item in an array shares one schema. Switch Arrays to By position when the array is a tuple, such as a coordinate pair. Draft 2020-12 writes prefixItems, and older drafts write an items array with additionalItems set to false.
Formats and enums
Detect formats adds a format keyword when every value of a field matches one: date-time, date, time, uuid, email, ipv4, ipv6 or uri. A single value that does not match removes the format, so a field is never labeled wrongly. Only formats that exist in the chosen draft are used. uuid appears from 2019-09, and date and time from draft 07.
Enums list the values of a text field when there are few of them and they repeat. The rule needs at least three values, and the number of different values must be at most half of the number seen. Three samples with two roles do not become an enum, because that could be chance. When a field can also be null, null is added to the enum so valid data still passes.
{"role":"user"} {"role":"admin"} {"role":"user"} {"role":"admin"}
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
role:
type: string
enum:
- user
- admin
required:
- roleChoosing a draft
The draft sets the $schema value and the keywords that are used. Draft 2020-12 is the current one and the right choice for new work. 2019-09 and draft 07 are still common in tools such as editors and API gateways. Draft 06 and draft 04 are for older systems, and draft 04 has no examples keyword, so examples are left out for it.
If you are writing an OpenAPI document, choose YAML under Output. The result can be pasted into components/schemas. OpenAPI 3.0 uses its own variant of JSON Schema, so check nullable and the type list against the version you target.
More options
- Allow extra properties: turn it off to write additionalProperties: false on every object, so unknown keys fail validation.
- Top-level array: treat a top-level array either as one array value or as a list of samples, so [{...},{...}] gives a schema for one item.
- Add titles: writes a title from each property name, so first_name becomes First Name.
- Add examples: keeps up to three example values from your samples for text and number fields.
- Add observed limits: writes minLength, maxLength, minimum, maximum, minItems and maxItems from the values seen. Only use it when the samples cover the real range, because the limits come from the data you pasted.
- Schema title: puts a title on the whole schema.
Limits and next steps
A schema inferred from samples describes the samples, not your intent. It cannot know that a number must be positive or that a field is a secret, so review it and add the rules that matter. The tool does not use $ref or $defs, and values nested deeper than 200 levels are left out with a note.
To test the schema against real data, open the JSON Schema Validator, which lists every error with its line and column. JSON to YAML converts a schema file you already have, Sort JSON Keys puts its keys in order, and JSONL Viewer helps you look through a large file of samples first.