Generate a JSON Schema (draft 2020-12) from a sample JSON document, ready to copy and refine.
What is JSON Schema Generator?
JSON Schema Generator is a free online tool that reads a sample JSON document in your browser and writes a JSON Schema (draft 2020-12) that describes it: the type of every value, the properties of every object, the items of every list and which properties are required. It is a starting point to refine, not a finished contract.
Use it to start a schema from a real API response or configuration file, then tighten it by hand.
What it supports
- Any JSON value as the sample: an object, a list or a single value.
- The result is a draft 2020-12 schema that starts with "$schema": "https://json-schema.org/draft/2020-12/schema".
- type is one of object, array, string, number, integer, boolean and null. A whole number such as 7 is an integer, and a number with a fractional part, such as 9.5, is a number. When both appear in the same place, the type is number.
- An object gets properties, one for each name, and required, which lists the names present in every object seen at that place. For a single object that is every name. required is left out when no name is in every object.
- A list gets items, one schema built from all of its items together. Objects in a list are merged: a name is required only if it is present in every one of them. An empty list gets {} as items.
- When the same place holds different kinds of value, type becomes a list sorted by name, such as ["integer", "null", "string"]. When integers and other numbers meet, the type is just number.
- The schema is written with the indentation you choose, 2 or 4 spaces.
Good to know
- The schema describes this one sample, so it is a starting point. It cannot know which properties are optional, which texts are dates or emails, or what range a number may have: every name is required unless another object in the sample lacks it, and no format, pattern, minimum or enum is added.
- additionalProperties is not set, so the schema accepts extra properties. Add "additionalProperties": false yourself if you want to forbid them.
- A list of several objects of the same kind gives a more useful schema than one object: put a few typical items in your sample.
- null in a sample gives the type null, not “a value that may be missing”. If a field can be a text or null, show both cases, for example in two items of a list.
- A name repeated inside one object, and a string with an unpaired surrogate escape, are refused instead of guessed.
How do I use JSON Schema Generator?
- Paste or type a sample JSON document in the input box. Nothing runs while you type.
- Choose the indentation (“2 spaces” or “4 spaces”) and press “Generate”.
- Read the schema next to your input. If the sample cannot be used, the message says why and gives the line and column.
- Press “Copy” to put the schema on your clipboard, or “Download” to save it as schema.json.
Examples
An object with several kinds of values
Paste this sample and press “Generate”. 7 is an integer and 9.5 is a number, the list tags gets items of type string, the nested object address gets its own properties and required, and null gives the type null. Every name in the sample is listed in required, because one sample cannot show which names are optional. The schema starts with a $schema line for draft 2020-12.
{
"id": 7,
"name": "Ada",
"active": true,
"score": 9.5,
"tags": [
"math",
"poetry"
],
"address": {
"city": "London"
},
"nickname": null
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"active": {
"type": "boolean"
},
"score": {
"type": "number"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"address": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
},
"required": [
"city"
]
},
"nickname": {
"type": "null"
}
},
"required": [
"id",
"name",
"active",
"score",
"tags",
"address",
"nickname"
]
}A name repeated in the sample
The name “id” appears twice in the same object. The tool refuses instead of choosing a value to describe, and gives the line and column of the name.
{"id": 1, "id": 2}An object repeats a member name. Programs disagree about which value wins, so this tool refused instead of keeping one value and dropping the other. Make the names unique and try again.
Line 1, column 2
A list of objects
The schema for the items is built from all the objects in the list together. id is in both, so it is required; name is in only one, so it is a property but not required.
[{"id": 1, "name": "Ada"}, {"id": 2}]{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
},
"required": [
"id"
]
}
}Lists with mixed or no items
In ids, a whole number and 2.5 are merged into one type, number. In mixed, three kinds of value give a list of types, sorted by name. The empty list has nothing to learn from, so its items are an empty schema, {}, which accepts anything.
{"ids": [1, 2.5], "mixed": [1, "a", null], "empty": []}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"ids": {
"type": "array",
"items": {
"type": "number"
}
},
"mixed": {
"type": "array",
"items": {
"type": [
"integer",
"null",
"string"
]
}
},
"empty": {
"type": "array",
"items": {}
}
},
"required": [
"ids",
"mixed",
"empty"
]
}Limits and privacy
Size limits
- Input: up to 2,000,000 bytes of UTF-8 text (about 2 MB). Accented letters and emoji take more than one byte each. Larger input is rejected.
- Result: also up to 2,000,000 bytes. If the complete result would be larger, the tool shows only an incomplete preview of the first 100,000 bytes, and that preview cannot be copied or downloaded.
- Nesting: up to 256 levels of objects and lists inside each other. Deeper JSON is refused because it is beyond what the tool processes.
- Time: a run that takes longer than its time limit is stopped. Try a smaller input.
Is my text sent to a server?
No. The text you type or paste, or open from a local file, is processed locally in your browser, in a dedicated Web Worker. It is never uploaded or sent to the server (a file you open is read in your browser only), and it is not written to storage, cookies or the address bar.
“Copy” puts the result on your clipboard and “Download” saves it as a file, but only when you press the button. The file is created in your browser, so nothing is uploaded.
For the full details, see the Privacy Policy
Related tools and pages
Your text stays in the page when you switch tools in the same tab, so you can try the same text in another tool. Reloading or closing the tab ends it.