JSON Schema Examples
Common JSON Schema patterns — required fields, enums, arrays, nested objects, refs.
Reference
Minimal object
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 }
},
"required": ["name"]
}Enum / string pattern
{
"type": "object",
"properties": {
"status": { "type": "string", "enum": ["active", "paused", "closed"] },
"email": { "type": "string", "format": "email" },
"slug": { "type": "string", "pattern": "^[a-z0-9-]+$" }
}
}Array of items
{
"type": "array",
"minItems": 1,
"maxItems": 10,
"uniqueItems": true,
"items": {
"type": "object",
"properties": { "id": { "type": "integer" } },
"required": ["id"]
}
}Nested objects with $ref
{
"$defs": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"zip": { "type": "string", "pattern": "^\\d{5}$" }
}
}
},
"type": "object",
"properties": {
"shipping": { "$ref": "#/$defs/address" },
"billing": { "$ref": "#/$defs/address" }
}
}oneOf — discriminated union
{
"oneOf": [
{ "type": "object", "properties": { "kind": { "const": "dog" }, "bark": { "type": "string" } }, "required": ["kind","bark"] },
{ "type": "object", "properties": { "kind": { "const": "cat" }, "meow": { "type": "string" } }, "required": ["kind","meow"] }
]
}Useful keywords
- type
- string | number | integer | boolean | null | array | object
- required
- Array of required property names
- additionalProperties
- false to reject unknown keys
- minLength / maxLength
- String constraints
- minimum / maximum
- Number constraints (exclusiveMinimum / exclusiveMaximum for open)
- format
- date-time, email, uri, uuid, ipv4, ipv6 (validator-specific)
- allOf / anyOf / oneOf
- Combinators
- const
- Match single value exactly
Last updated: