Generate a JSON Schema from One or Several Sample Documents
Generate a JSON Schema from JSON:
paste one sample or several, and fields missing from any sample become optional.
Paste JSON or open a file, choose Draft 2020-12 or Draft-07, and the schema appears next to your sample.
It reads one document or many. With several samples it merges them, so the schema shows which fields
are always there, which are optional and which change type from one sample to the next.
Already have a schema? Feed it into the Mock JSON Data Generator
to produce fake test data that matches the shape you just generated.
How to Use It
- Paste your JSON into the left editor, open a
.json, .jsonl or .txt file with Open file, or drop a file on the editor. Sample loads an example.
- Choose Samples. One document reads a single JSON value. Several documents reads many samples at once and merges them.
- Choose options: draft, required fields, additional properties, string formats, and the extras (reusable definitions, examples, titles, enums).
- Click Generate, then copy or download the schema as
schema.json, or use Check samples against schema to test it.
How the Schema Is Inferred
The generator reads every value it is given and records what it saw at each place: which types occurred,
which keys were present in how many objects, what the items of each array looked like, and which strings
could still be dates, e-mail addresses or other formats. It then writes the schema from those records.
Because it works from everything it saw and not from the first value, the same properties, required list
and types come out whatever the order of your samples. Only the order inside a type list or an
anyOf follows the order in which kinds were first seen.
A value becomes string, boolean, null, object or
array. A number is integer when its value is whole (5,
5.0 and 1E5 all count) and number otherwise. Objects become
properties. Every array becomes "type": "array" with one items schema
that merges all of its elements, in every sample. An empty array gives "items": {}, because
nothing is known about its items. The output uses only type, properties,
required, items, anyOf, additionalProperties,
format, enum, examples, title and the definitions
keyword. It does not guess minimums, maximums, lengths or patterns.
Numbers keep their spelling. A value such as 12345678901234567890 or 2.50 is read
exactly as typed, so it appears unchanged if you add examples or enums, and a long integer is still typed
as an integer.
Several Documents: Input Formats
Set Samples to Several documents when you have more than one example. Three layouts are accepted.
JSON Lines has one JSON value per line. Values one after another can be
separated by spaces, blank lines, or a line that contains only ---. And one top-level
array can be read as separate samples when you tick Treat top-level array items as separate
samples; without the box, an array is a single sample. A file ending in .jsonl or
.ndjson switches the page to Several documents when you open it.
A syntax error names the document, the line and the column. For example, a trailing comma on the third
line of a JSON Lines file, {"id": 3,}, gives: Document 3, line 3, column 10: Expected a property name in double quotes but found '}'.
Required or Optional? A Two-Sample Example
Properties are merged across samples: the schema describes the union of all keys. A key goes into
required only if it is present in every sample. In the example below, both people
have id and name, but only Ada has a phone, so phone
is described but not required.
Two samples (JSON Lines)
{"id": 1, "name": "Ada", "phone": "555-0101"}
{"id": 2, "name": "Grace"}
Schema (Several documents, defaults)
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Generated schema",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"phone": {
"type": "string"
}
},
"required": [
"id",
"name"
]
}
Mark all properties as required changes this. In One document mode, every key of the sample
becomes required (inside an array of objects, only the keys that every item has). In Several documents mode
it requires every key seen in any sample, so a sample that lacks one of those keys would fail the
schema. Leave it off when your samples vary. The page switches it off for you when you choose Several
documents, unless you have changed the box yourself. With the box off, Several documents mode requires the
keys present in every sample, and One document mode writes no required list at all.
Nullable and Mixed Types
When a field is one type in some samples and null in others, the generator writes a type array
such as "type": ["string", "null"]. That form is valid in both Draft-07 and Draft 2020-12, so no
OpenAPI-style nullable keyword is written. A field that is missing from some samples is optional,
not nullable: in the example, middleName is null once and missing once, so it is not required
and may be a string or null.
Three samples
{"id": 1, "middleName": "Marie"}
{"id": 2, "middleName": null}
{"id": 3}
Schema: middleName is optional and nullable
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Generated schema",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"middleName": {
"type": [
"string",
"null"
]
}
},
"required": [
"id"
]
}
Several primitive types give a type array in the order they were first seen, so null comes first if the first sample has a null.
An integer together with a decimal number becomes number, since every integer is also a number.
When a field mixes objects or arrays with other types, the generator writes anyOf with one
branch per kind. Samples of the same kind are merged into one branch, so two objects with different keys
become a single object branch with the union of their keys.
Four samples with different kinds of value
{"value": 42}
{"value": "42"}
{"value": {"unit": "kg", "amount": 42}}
{"value": null}
Schema: one anyOf branch per kind
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Generated schema",
"type": "object",
"properties": {
"value": {
"anyOf": [
{
"type": "integer"
},
{
"type": "string"
},
{
"type": "object",
"properties": {
"unit": {
"type": "string"
},
"amount": {
"type": "integer"
}
},
"required": [
"unit",
"amount"
]
},
{
"type": "null"
}
]
}
},
"required": [
"value"
]
}
Draft-07 and Draft 2020-12: What Differs Here
- Dialect URI. The
$schema line names the draft you picked.
- Definitions. Draft 2020-12 stores reusable pieces under
$defs and points to them with #/$defs/Name. Draft-07 uses definitions and #/definitions/Name.
- Siblings of
$ref. In Draft-07 any keyword next to a $ref is ignored, so with Add titles the generator does not put a title beside a reference in Draft-07. Draft 2020-12 allows it.
- Tuple arrays. Draft 2020-12 describes arrays with fixed positions using
prefixItems, while Draft-07 uses items as an array. The generator does not write tuple arrays. It treats every array as a list whose items share one merged schema, so [1, "a"] gives items of type integer or string. If your arrays are positional, edit that part by hand.
Everything else the tool writes (types, properties, required, items, anyOf, enum, format) is the same in both drafts.
When to Extract Repeated Object Shapes
Extract repeated object shapes is off by default. Turn it on when the same object appears in more
than one place, such as a billing and a shipping address, or the items of two lists. Objects with identical
structure (the same properties, the same schemas and the same required list; property order does not matter)
that occur at least twice are moved into the definitions keyword and replaced with a $ref.
Shapes that are merely similar stay where they are. Each definition is named in PascalCase after the first
property it was found under (billing becomes Billing), and a second shape with the
same name gets a number: Billing2.
One document, Extract repeated object shapes on
{"billing": {"street": "1 Main St", "city": "Oslo"}, "shipping": {"street": "9 Side Rd", "city": "Bergen"}, "note": "gift"}
Schema (Draft 2020-12)
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Generated schema",
"type": "object",
"properties": {
"billing": {
"$ref": "#/$defs/Billing"
},
"shipping": {
"$ref": "#/$defs/Billing"
},
"note": {
"type": "string"
}
},
"required": [
"billing",
"shipping",
"note"
],
"$defs": {
"Billing": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
}
},
"required": [
"street",
"city"
]
}
}
}
The result is shorter and has one place to edit. The name comes from the first use, so two unrelated objects
that happen to have identical fields will share one definition; rename it if the name is misleading.
String Formats and Their Limits
With Detect string formats on, the generator can write nine formats: date-time
(such as 2026-01-15T10:30:00Z, with a real calendar date), date,
time (with a zone, such as 10:30:00Z), email, uuid,
ipv4, ipv6, uri (an absolute address with ://) and
hostname (ASCII, at least two labels). The rule is strict: a format is written only if
every string seen at that place, across all array items and all samples, matches. One value that
does not match, such as an empty string or n/a, means no format at all. A nullable field keeps
its format, for example {"type": ["string", "null"], "format": "date"}. A value that fits
two formats gets the first of this list: date-time, date, time, email, uuid, ipv4, ipv6, uri, hostname.
The checks are deliberately simple and are not full RFC parsers. They do not accept internationalised
addresses or host names, quoted e-mail names or mailto: links. Also, many validators treat
format as a hint unless format checking is switched on, so do not rely on it to reject bad
data unless you have configured your validator to assert formats.
Examples, Titles and Enums: What Can and Cannot Be Inferred
Add examples copies up to three distinct values per field, in the order they were first
seen, into examples. Strings longer than 60 characters are cut at 60. Because examples copy
your values into the schema, check the output before sharing it if the samples contain personal or
confidential data. Add titles turns property names into readable titles
(firstName becomes First name, home_address becomes Home address).
Infer enums writes an enum only for string or integer fields that were
observed at least 20 times with at most 5 distinct values, each seen at least twice.
An inferred enum is a guess. Twenty rows containing new, open and
closed say nothing about a value that has not appeared yet, such as blocked. Remove
the enum when new values may be added later. The same goes for formats and types: the tool can only report
what it saw.
Open a File, Large Inputs and Limits
Open file accepts .json, .jsonl, .ndjson and
.txt, and you can also drop a file onto the left editor. The page shows the file name and
size. Reading and generating happen in a background worker, so a large file does not freeze the page, and a
Cancel button appears if it takes longer than a second. A file over 1.5 MB is not shown in the editor
(the box says "Large file loaded") and the schema is generated straight from the file. Files up to 50 MB
are accepted; above that the page asks you to split the file. Objects and arrays may be nested up to 500
levels. Text pasted into the box is limited to 20 million characters, and for anything over a few
megabytes Open file is the better route.
Checking Samples Against the Schema
Check samples against schema runs your samples through the generated schema. On success it says the
samples are valid. On failure it lists the first 20 errors, each with the JSON Pointer of the failing place.
In Several documents mode every sample is checked and the failing document numbers are listed. If you left
Mark all properties as required on in that mode, samples that lack a key will fail, which is exactly
what that option asks for. Open in JSON Schema Validator sends the sample and schema to the
JSON Schema validator page for a closer look (One document mode).
A Schema Inferred from Samples Describes the Samples
The tool can only describe the data you give it, not your intent. Review the result before you use it. A
field that is always an integer in your samples may later hold decimals, a field that is always present may
be optional in the real API, and a field that was never null may become null. Use more samples, including the
awkward ones (empty arrays, nulls, missing optional fields), and add the constraints that samples cannot
show, such as minimum, maxLength and pattern, by hand. A good flow is
to generate, review, check real documents with the validator, and then build on the schema with the
code generator or the mock data generator.