How to write a good JSON Schema
A JSON Schema is a contract: it says exactly what shape your data must have, and lets a validator check that shape before anything breaks. A good schema is strict where it matters and forgiving where it does not. It is also readable: you or a teammate should understand what each field means without digging through the code that produces the data. The habits below work for hand-written schemas and for schemas a tool generates.
Start with the shape, not the details
Before you write a single keyword, sketch the data: the top-level object, its fields, and which of those are objects or arrays themselves. A schema written top-down stays organised; one written field by field grows into a flat list of unrelated properties. Decide early what type each field has — string, number, integer, boolean, array, object, or null. The type keyword is the first line of defence. If a field can hold two types, say so with a list, such as ["string", "null"], instead of leaving the type out and hoping for the best.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/user.schema.json",
"title": "User",
"type": "object",
"additionalProperties": false,
"required": ["name", "email"],
"properties": {
"name": {
"type": "string",
"minLength": 1,
"description": "Full name of the user, as printed on the invoice."
},
"email": {
"type": "string",
"format": "email",
"description": "Contact email address."
},
"age": {
"type": "integer",
"minimum": 0,
"description": "Age in years. Omit the field when unknown."
},
"nickname": {
"type": "string",
"minLength": 1,
"description": "Display name, optional."
}
}
}
required is a decision, not a default
Every field in properties is optional unless you list it in required. That is a feature, but only if you choose deliberately: can the data exist without this field? An order without a total is broken; a user profile without a nickname is fine. Mark the fields that must be there and leave the rest optional. The common mistake is the opposite — forgetting required entirely, so an empty object {} passes validation and the error surfaces three systems later, far from the cause. And remember: required only says the key must be present, not that its value is valid.
Write descriptions a stranger could use
A schema is read by people as well as by validators, and by language models. A description should say what the field means, not repeat its name. "name" tells you nothing; "Full name of the customer, as printed on the invoice" tells you everything. Add the unit when it matters — "Price in Canadian dollars, including taxes" beats a bare "price" — and state the format when a string has one, such as an ISO date. Clear descriptions also help generators produce better data, and teammates ask fewer questions. Keep them to one sentence, written for whoever will debug this data at midnight.
Close the door with additionalProperties
By default, an object schema accepts any extra properties, even typos. A payload with "emial" instead of "email" passes validation, and the real address is silently lost. Setting "additionalProperties": false closes that door: anything not listed in properties is rejected, and the typo is caught early instead of in production. Use it on every object you control. The one exception is data you genuinely cannot predict, such as free-form metadata: keep it open but constrain the values, for example with "additionalProperties": { "type": "string" }, so unknown keys are allowed but unknown value types are not.
{
"type": "object",
"additionalProperties": { "type": "string" },
"properties": {
"source": { "type": "string" }
}
}
Split big objects into pieces
When an object grows past a handful of fields, or when the same group of fields appears in more than one place, split it out. A postal address, a money amount, a date range — each deserves its own definition, reused with $ref. Splitting keeps the top-level schema short, gives each part a documenting name, and means a fix in one place propagates everywhere. In JSON Schema 2020-12, reusable pieces live under $defs at the root, and a property refers to one with { "$ref": "#/$defs/Address" }. Named pieces are also easier to test on their own.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Order",
"type": "object",
"additionalProperties": false,
"required": ["order_id", "shipping_address"],
"properties": {
"order_id": { "type": "string", "minLength": 1 },
"shipping_address": { "$ref": "#/$defs/Address" },
"billing_address": { "$ref": "#/$defs/Address" }
},
"$defs": {
"Address": {
"type": "object",
"additionalProperties": false,
"required": ["street", "city", "country"],
"properties": {
"street": { "type": "string", "minLength": 1 },
"city": { "type": "string", "minLength": 1 },
"country": { "type": "string", "pattern": "^[A-Z]{2}$" }
}
}
}
}
Test with real data
A schema you have never run against real data is a guess. Paste it into the Validator with a few genuine payloads — including the awkward ones: the empty string, the missing optional field, the null where you expected a number. Then run the Quality checker: it scores descriptions, required lists, and additionalProperties, and points at the gaps you stopped seeing. Finally, generate sample data and read it as a stranger would — if it looks wrong, the schema is wrong.
Conclusion
Good schemas are not clever; they are clear, strict where it matters, and tested against real data. Start from the shape, decide required deliberately, describe every field in plain words, close the door on typos, and split what grows. Then check your work in the Validator, the Quality checker, and the Sample data generator.
Keep reading
- JSON Schema vs Zod vs Pydantic — three ways to validate, and when to pick each one.
- Common schema mistakes — six traps, each with a before and after.
- All guides — back to the guides index.