How to generate a JSON Schema
- Paste a sample into Sample JSON. The schema appears as you type.
- Set Title. It becomes the schema’s
titlein PascalCase:order itemturns intoOrderItem. - Download saves a
.jsonfile named after the title.
A syntax error in the sample shows its line and column in place of the schema.
Example
With Title set to Order:
{
"orderId": 7731,
"items": [
{ "sku": "BAG-07", "qty": 1, "price": 54, "note": null },
{ "sku": "SOCK-3P", "qty": 2, "price": 12.5, "note": "gift wrap", "discount": 0.1 }
],
"coupon": null
}
Generates:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Order",
"type": "object",
"properties": {
"orderId": {
"type": "integer"
},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"sku": {
"type": "string"
},
"qty": {
"type": "integer"
},
"price": {
"type": "number"
},
"note": {
"type": [
"string",
"null"
]
},
"discount": {
"type": "number"
}
},
"required": [
"sku",
"qty",
"price",
"note"
],
"additionalProperties": false
}
},
"coupon": {}
},
"required": [
"orderId",
"items",
"coupon"
],
"additionalProperties": false
}
From the two items:
priceis anumber:54alone givesinteger, but12.5widens it.noteallows["string", "null"]:nullin one item, text in the other.discountis notrequired: only the second item has it.couponis{}, which accepts anything, because the sample only showsnull. Put the real type there.
An empty array becomes "items": {}, which accepts any item. A field that is a number in one item and a string in another becomes an anyOf of both.
Every object gets "additionalProperties": false, so unknown fields fail. Remove it where an API may add fields.
What a JSON Schema generator cannot infer
One sample shows types, not rules. A timestamp and an email address are both "type": "string", so add "format": "date-time" or "format": "email" yourself, plus minimum, maximum, pattern and enum where they apply. Nested objects are written inline rather than as shared $defs, even when two match.
Test it
Check the schema in the JSON Schema validator against real documents, including some that should fail. For types instead, JSON to TypeScript and JSON to Zod read the same sample; Zod also validates at runtime.