JSON Schema Generator
Generate a JSON Schema (draft 2020-12) from a sample JSON document, locally in your browser.
Generate a JSON Schema (draft 2020-12) from a sample JSON document, locally in your browser.
Paste a JSON sample and press Generate. The JSON Schema generator parses the sample, infers a type for every value at every depth, and writes a schema that describes it: nested objects get their own properties and required lists, arrays get an items schema, whole numbers become integer, and values that are sometimes null get a type pair such as ["integer", "null"].
The output is a solid first draft of a contract for an API response, a webhook body or a config file. It records the shape of your data; you then add the business rules a sample cannot show.
schema.json, and refine it as described below.| In the sample | In the schema |
|---|---|
"Ada" | {"type": "string"} |
37 | {"type": "integer"} |
19.5, or whole and decimal numbers mixed across records | {"type": "number"} |
true | {"type": "boolean"} |
| an object | "type": "object" with nested properties and required |
| an array | "type": "array" with one items schema merged from every element |
a value that is null in some records | a type pair such as ["integer", "null"] |
a value that is only ever null, an empty array's items, or mixed types | {}, which accepts anything |
{"id":7,"name":"Ada","price":19.5,"active":true,"address":{"city":"London","zip":"N1"},"tags":["admin","editor"]}Generated schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Root",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"price": {
"type": "number"
},
"active": {
"type": "boolean"
},
"address": {
"type": "object",
"properties": {
"city": {
"type": "string"
},
"zip": {
"type": "string"
}
},
"required": [
"city",
"zip"
]
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"id",
"name",
"price",
"active",
"address",
"tags"
]
}Every key is required because the generator saw it once and has no evidence it can be missing.
{"users":[{"name":"Ada","age":36,"nick":"ada"},{"name":"Bo","age":null,"score":1.5}]}Generated schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Root",
"type": "object",
"properties": {
"users": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": [
"integer",
"null"
]
},
"nick": {
"type": "string"
},
"score": {
"type": "number"
}
},
"required": [
"name",
"age"
]
}
}
},
"required": [
"users"
]
}Merging the two records shows that nick and score are optional (absent from one record each), so they are not in required, and that age may be null.
The input [1, "two", 3] has no single item type, so the items schema is left open:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Root",
"type": "array",
"items": {}
}
required. Remove keys that real clients may omit, especially when you generated from a single object.format (email, date-time, uuid), pattern, enum, minimum, maxLength and minItems for rules the sample cannot reveal.additionalProperties, so unknown keys are allowed. Add "additionalProperties": false for strict request validation.{} entries. A field that was always null accepts anything until you give it a real type.Root title and add description fields; documentation generators show them.Generate a schema when you need one contract that several languages and tools can share: Ajv in Node.js, jsonschema in Python, OpenAPI component definitions, or editor autocompletion for a config file. If you only need types in one language, generate them directly with JSON to TypeScript, JSON to Pydantic or JSON to Go. To start from XML or YAML instead of JSON, choose that input format on the class generator hub, which can also emit JSON Schema.
Draft 2020-12. The document starts with "$schema": "https://json-schema.org/draft/2020-12/schema" and a "title" of Root. The keywords it uses also mean the same in draft-07, so you can change the $schema URL if your validator only supports the older draft.
A key is required when it appears in every object the generator saw at that position. With a single sample object every key is required; with an array of records, keys missing from any record are left out of required.
No. Strings are typed as string only. Add format, pattern, enum, minLength and numeric bounds yourself once you know the rules.
Put the samples in a JSON array and generate from that. The item schemas are merged, so optional keys and nullable values show up, and you can copy the items schema as your object schema.
Yes. It describes the sample, so the sample always validates. Until you add constraints it will also accept many payloads you would want to reject.
No. Parsing and schema generation run in your browser with JavaScript, and the sample never leaves the page.