JSON to JSON Schema Generator - Draft 2020-12
A first draft of the schema
Writing a JSON Schema from nothing means listing every property and type by hand. If you already have example payloads, this does the first pass: paste the JSON and it writes a schema that the sample passes. Then you tighten it. It uses the same type inference as our code generators, so array items are merged into one shape and the required list comes from what was actually present.
Example
Input, with the schema title set to UserList:
{
"users": [
{ "id": 1, "email": "ada@example.com", "joined": "2024-03-01T09:30:00Z", "tags": ["admin"] },
{ "id": 2, "email": null, "joined": "2024-05-12T14:02:00Z", "tags": [], "manager": 1 }
]
} Output with the default options:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "UserList",
"type": "object",
"properties": {
"users": {
"type": "array",
"items": {
"$ref": "#/$defs/User"
}
}
},
"required": ["users"],
"$defs": {
"User": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"email": {
"type": ["string", "null"],
"format": "email"
},
"joined": {
"type": "string",
"format": "date-time"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"manager": {
"type": "integer"
}
},
"required": ["id", "email", "joined", "tags"]
}
}
} manager was only on the second user, so it isn't in required. email is
required but nullable. The empty tags array on the second user didn't erase the
string item type learned from the first.
Options
Draft switches between 2020-12 and draft-07. Named $defs puts each nested object
under $defs (definitions in draft-07) and points to it with $ref; untick it
to get one nested schema with everything written inline, which some form generators and LLM structured-output
APIs handle better. Detect formats adds format for strings that look like
date-time, date, email, uri or uuid. A format
is only kept if every sample of that field matched it, so one free-text value drops it. Unticking
Allow extra properties adds "additionalProperties": false to every object.
Using it with OpenAPI and validators
OpenAPI 3.1 uses 2020-12 as its schema dialect, so you can paste the objects under $defs into
components/schemas and change each $ref from #/$defs/User to
#/components/schemas/User. OpenAPI 3.0 is different: it doesn't accept type arrays, so
"type": ["string", "null"] has to become "type": "string", "nullable": true. The
generator doesn't do that rewrite for you.
In code, Ajv takes the schema as-is: new Ajv2020().compile(schema) for 2020-12, or the default
Ajv export for draft-07, plus addFormats(ajv) if you kept the formats. In Python,
jsonschema.Draft202012Validator(schema).iter_errors(doc) lists every problem.
What to add by hand
Fields with values of different types become a type array such as ["integer", "string"], or an
anyOf when one of them is an object or array. Fields that were null in every sample get
{}, which accepts anything, so search for those first. After that, add the rules a sample
can't show: enum for status fields, minimum for quantities, pattern for
IDs, and description text for anyone reading the schema later.
To check documents against the result, paste it into the JSON Schema validator. If the consumer is TypeScript code, JSON to Zod gives you a runtime validator from the same sample, and JSON to TypeScript gives you the static types.
Same JSON, other languages
Every generator here reads the sample the same way, so a key that is optional or nullable in one language is optional or nullable in the others. Paste the same input into: TypeScript, Zod, Go, Python, Rust, Java, C#.
Related Tools
JSON to YAML Converter
Convert JSON data to clean, readable YAML format instantly.
JSON to XML Converter
Transform JSON objects and arrays into well-formed XML documents.
JSON to CSV Converter
Flatten JSON arrays into downloadable CSV spreadsheets in one click.
YAML to JSON Converter
Convert YAML configuration files to JSON format instantly.