JSON Schema Cheat Sheet
JSON Schema reference with types, validators, composition, references, and common validation patterns. Copy-ready examples.
Basics
| Syntax | Description | Example |
|---|---|---|
| Declare JSON Schema version | "$schema": "https://json-schema.org/draft/2020-12/schema" | |
| Unique identifier for the schema | "$id": "https://example.com/user.schema.json" | |
| Human-readable schema name | "title": "User" | |
| Schema description | "description": "A registered user" | |
| Expected data type | "type": "object" | |
| Developer comment (ignored by validators) | "$comment": "TODO: add email validation" | |
| Example values for documentation | "examples": ["alice@example.com"] | |
| Default value | "default": "active" | |
| Mark as deprecated (2019-09+) | "deprecated": true |
Types
| Syntax | Description | Example |
|---|---|---|
| String type | { "type": "string" } | |
| Number (integer or float) | { "type": "number" } | |
| Integer only | { "type": "integer" } | |
| Boolean (true/false) | { "type": "boolean" } | |
| Array type | { "type": "array", "items": { "type": "string" } } | |
| Object type | { "type": "object", "properties": { ... } } | |
| Null type | { "type": "null" } | |
| Nullable type (union) | { "type": ["string", "null"] } |
String
| Syntax | Description | Example |
|---|---|---|
| String length constraints | "minLength": 1, "maxLength": 255 | |
| Regex pattern for string | "pattern": "^[a-zA-Z]+$" | |
| Email format | "format": "email" | |
| URI format | "format": "uri" | |
| ISO 8601 date | "format": "date" | |
| ISO 8601 datetime | "format": "date-time" | |
| UUID format | "format": "uuid" | |
| IP address format | "format": "ipv4" | |
| Hostname format | "format": "hostname" | |
| MIME type of string content | "contentMediaType": "application/json" |
Number
| Syntax | Description | Example |
|---|---|---|
| Inclusive range | "minimum": 0, "maximum": 100 | |
| Exclusive range | "exclusiveMinimum": 0 | |
| Must be multiple of value | "multipleOf": 5 |
Array
| Syntax | Description | Example |
|---|---|---|
| Schema for array elements | "items": { "type": "string" } | |
| Tuple validation (2020-12) | "prefixItems": [{ "type": "string" }, { "type": "number" }] | |
| Array length constraints | "minItems": 1, "maxItems": 10 | |
| All items must be unique | "uniqueItems": true | |
| At least one item matches schema | "contains": { "type": "number" } | |
| Count of matching contains items | "contains": { ... }, "minContains": 2 |
Object
| Syntax | Description | Example |
|---|---|---|
| Define expected properties | "properties": { "name": { "type": "string" } } | |
| List of required properties | "required": ["name", "email"] | |
| Allow/restrict extra properties | "additionalProperties": false | |
| Schema for extra properties | "additionalProperties": { "type": "string" } | |
| Property count constraints | "minProperties": 1 | |
| Properties matching regex pattern | "patternProperties": { "^x-": { "type": "string" } } | |
| Schema for property names | "propertyNames": { "pattern": "^[a-z]+$" } | |
| Conditional required properties | "dependentRequired": { "credit_card": ["billing_address"] } | |
| Conditional schema based on property | "dependentSchemas": { "type": { ... } } |
Composition
| Syntax | Description | Example |
|---|---|---|
| Must match ALL schemas | "allOf": [{ "type": "string" }, { "minLength": 1 }] | |
| Must match at least ONE schema | "anyOf": [{ "type": "string" }, { "type": "number" }] | |
| Must match EXACTLY one schema | "oneOf": [{ "minimum": 0 }, { "maximum": -1 }] | |
| Must NOT match schema | "not": { "type": "string" } | |
| Conditional schema | "if": { ... }, "then": { ... }, "else": { ... } |
References
| Syntax | Description | Example |
|---|---|---|
| Reference another schema | "$ref": "#/$defs/address" | |
| Define reusable schemas | "$defs": { "address": { "type": "object", ... } } | |
| Reference external schema | "$ref": "./address.schema.json" | |
| Reference remote schema | "$ref": "https://example.com/schemas/address" | |
| Dynamic references (2020-12) | "$dynamicRef": "#node" |
Enum
| Syntax | Description | Example |
|---|---|---|
| Value must be one of listed values | "enum": ["active", "inactive", "banned"] | |
| Value must be exactly this | "const": "pending" |
Frequently asked questions
What JSON Schema version should I use?
Use Draft 2020-12 for new projects. It's the latest version with the cleanest syntax (prefixItems for tuples, $dynamicRef). Draft 7 and 2019-09 are still widely supported. Check your validator's compatibility before choosing.
What's the difference between oneOf and anyOf?
anyOf passes if the value matches at least one schema. oneOf passes if the value matches EXACTLY one schema (and fails if it matches two or more). Use oneOf for discriminated unions, anyOf for flexible validation.
How do I validate nested objects?
Use 'properties' with nested schemas: { "type": "object", "properties": { "address": { "type": "object", "properties": { "city": { "type": "string" } } } } }. For deep nesting, extract schemas into $defs and use $ref.
Is format validation automatic?
No! By spec, format is an annotation, not a validator. Most libraries have an option to enable format validation (e.g., Ajv requires ajv-formats plugin). Always check your validator's documentation and explicitly enable format assertion.
How do I make a field nullable?
Use type array: { "type": ["string", "null"] }. This allows the field to be either a string or null. In Draft 2019-09+, you can also use anyOf with null type.
Can I generate code from JSON Schema?
Yes. Tools like quicktype generate TypeScript/Java/Python types from schemas. json-schema-to-typescript is popular for TypeScript. OpenAPI (which uses JSON Schema) has extensive code generation tools (openapi-generator).
Go from reference to real skills
Cheat sheets are great for quick lookups. Our in-depth courses take you from the fundamentals to professional-level mastery.
Browse all courses