JSON Schema is a JSON document that describes other JSON documents. Validators use it to accept or reject data, editors use it for autocomplete, code generators turn it into types, and in 2026 it is also how you tell an LLM exactly what to return.
The smallest useful schema
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"email": { "type": "string", "format": "email" },
"name": { "type": "string", "minLength": 1 }
},
"required": ["email", "name"],
"additionalProperties": false
}
$schemanames the dialect. Always include it.propertiesdescribes fields.requiredlists those that must be present. Properties are optional by default.additionalProperties: falserejects unknown keys, which catches typos likeemial.
Constraining values
| Keyword | Applies to | Example |
|---|---|---|
enum / const | any | {"enum": ["admin", "editor"]} |
minLength / maxLength / pattern | strings | {"pattern": "^[A-Z]{2}$"} |
minimum / maximum / multipleOf | numbers | {"minimum": 0} |
minItems / maxItems / uniqueItems | arrays | {"maxItems": 20} |
format | strings | email, uuid, date-time, uri |
Nullable fields
"nickname": { "type": ["string", "null"] }
A type array is the 2020-12 way. nullable: true was an OpenAPI 3.0 invention and is not valid JSON Schema.
Reuse with $defs and $ref
"properties": { "address": { "$ref": "#/$defs/address" } },
"$defs": {
"address": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
$defs replaced the older definitions keyword. $ref takes a JSON Pointer into the same document, or a URI pointing at another schema.
Composition
oneOf: exactly one sub-schema matches. Use it for tagged unions, ideally with aconstdiscriminator field.anyOf: at least one matches.allOf: all match. Use it to layer constraints.if/then/else: conditional rules, such as "if country is US, zip is required".
Why 2020-12 is everywhere now
- OpenAPI 3.1+ schemas are 2020-12 schemas. No more separate dialect.
- MCP tool
inputSchema/outputSchemamoved to full 2020-12 in 2026. - LLM structured outputs from Claude, OpenAI and Gemini constrain generation to a schema you supply, though each supports only a subset of keywords.
One schema can now drive your API docs, your agent's tool contract and your model's output format.
Security notes
- Never let a validator dereference remote
$refURLs from a schema you don't control. - Bound sizes (
maxLength,maxItems) on any public input to avoid oversized payloads. - A valid document is only well-shaped. You still need to check permissions and business rules.
Open the JSON Schema template and ask the workspace to "add an array of addresses using $defs" or "make phone optional with a pattern", then review the diff.