How can I catch new posts?
Bookmark this blog and watch the homepage and tools hub—we surface new guides there. No account or mailing list is required to read articles.
Build practical JSON Schema contracts with copy-ready examples for required fields, nested objects, arrays, enums, reusable definitions, and conditional validation.
JSON Schema describes the shape and constraints of JSON data. It can document an API contract, validate configuration files, protect an import pipeline, and turn assumptions such as “status is required” into executable rules. The examples below use modern JSON Schema vocabulary and are intentionally small enough to adapt.
This schema accepts an object with a required string id and a required status. additionalProperties: false catches misspelled or unexpected keys.
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"required": ["id", "status"],
"properties": {
"id": { "type": "string", "minLength": 1 },
"status": { "enum": ["pending", "active", "disabled"] }
},
"additionalProperties": false
}Use additionalProperties: false deliberately. It is useful for controlled configuration and internal APIs, but public APIs may need to tolerate new fields for backward compatibility.
Nested rules belong inside the relevant property. Here the address itself is required, and two fields inside it are also required.
{
"type": "object",
"required": ["name", "address"],
"properties": {
"name": { "type": "string" },
"address": {
"type": "object",
"required": ["country", "postalCode"],
"properties": {
"country": { "type": "string", "minLength": 2 },
"postalCode": { "type": "string", "pattern": "^[A-Za-z0-9 -]+$" }
},
"additionalProperties": false
}
}
}A regex checks text shape, not real-world existence. If postal codes must be valid for a specific country, keep that business rule outside the generic schema or use carefully tested conditional schemas.
Use items to describe every element, and array keywords to control size and duplicates.
{
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"type": "object",
"required": ["sku", "quantity"],
"properties": {
"sku": { "type": "string", "minLength": 1 },
"quantity": { "type": "integer", "minimum": 1 }
},
"additionalProperties": false
}
}Remember that uniqueItems compares complete JSON values. Two objects with the same sku but different quantities are still different objects; enforcing uniqueness by one field usually requires application logic.
$defsRepeated structures should have one source of truth. Define them under $defs and reference them with $ref.
{
"$defs": {
"money": {
"type": "object",
"required": ["amount", "currency"],
"properties": {
"amount": { "type": "number", "minimum": 0 },
"currency": { "type": "string", "pattern": "^[A-Z]{3}$" }
},
"additionalProperties": false
}
},
"type": "object",
"properties": {
"subtotal": { "$ref": "#/$defs/money" },
"total": { "$ref": "#/$defs/money" }
}
}For financial systems, consider representing decimal amounts as validated strings or integer minor units so binary floating-point behavior is explicit.
if, then, and else are useful when one field changes which other fields are required.
{
"type": "object",
"required": ["deliveryMethod"],
"properties": {
"deliveryMethod": { "enum": ["pickup", "shipping"] },
"shippingAddress": { "type": "string", "minLength": 1 },
"pickupLocationId": { "type": "string", "minLength": 1 }
},
"allOf": [
{
"if": { "properties": { "deliveryMethod": { "const": "shipping" } } },
"then": { "required": ["shippingAddress"] },
"else": { "required": ["pickupLocationId"] }
}
]
}Keep conditional schemas readable. If the rules begin to model permissions, inventory, or workflow state, perform those checks in application code and use Schema for the structural contract.
The official JSON Schema documentation is the reference for vocabulary and draft behavior. A generator accelerates the first draft, but the final schema must reflect business intent and compatibility requirements.
Dedicated to providing developers with the best JSON processing tools
More posts coming soon...
Back to BlogFollowing the blog, topics we cover, and how to suggest guides.
Bookmark this blog and watch the homepage and tools hub—we surface new guides there. No account or mailing list is required to read articles.
JSON validation, formatting, conversion, debugging workflows, and JSON Work releases—mapped to what the free on-site tools can do locally in your browser.
Yes. Reach out via the About page or GitHub; we prioritize guides tied to real integration and debugging scenarios.