Nested Object Schema Generation from JSON
Generate JSON Schema for deeply nested JSON objects with sub-objects. Learn how the generator recurses into child properties to build a complete schema.
Detailed Explanation
Generating Schemas for Nested Objects
Real-world JSON is rarely flat. The generator must recurse into nested objects and produce a schema that mirrors the full depth of the data structure.
How Nested Generation Works
Given input like:
{
"user": {
"name": "Alice",
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "62704"
}
}
}
The generator produces a schema with nested properties blocks:
{
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"name": { "type": "string" },
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"zip": { "type": "string" }
},
"required": ["street", "city", "zip"]
}
},
"required": ["name", "address"]
}
},
"required": ["user"]
}
Depth Handling
Each nested object is treated as its own mini-schema with its own properties and required arrays. The generator applies the same type-inference logic at every level, so strings, numbers, booleans, arrays, and even deeper objects are all handled recursively.
When to Use $ref Instead
If the same sub-object shape appears in multiple places (e.g., a billingAddress and shippingAddress that share the same structure), inline nesting leads to duplication. In those cases, extract the shared shape into a $defs block and reference it with $ref. See the $ref and Definitions example for details.
Tips for Large Nested Structures
- Flatten where possible: Deeply nested schemas are harder to maintain. Consider whether a flatter structure serves your API better.
- Add descriptions: Nested properties are less self-documenting. Annotate each level with
descriptionfields. - Validate incrementally: Test each nested level independently before assembling the full schema, especially for 4+ levels of depth.
Nested object schemas are the backbone of complex API contracts, configuration files, and data interchange formats.
Use Case
Generate nested schemas when modeling user profiles with embedded addresses, order objects with line items, or any hierarchical data structure returned by REST or GraphQL APIs.
Try It — JSON Schema Generator
Related Topics
Generate a Simple Object Schema from JSON
Basic Types
Required vs Optional Properties in JSON Schema
Object Schemas
$ref and $defs — Reusable Schema Definitions
Advanced Patterns
Controlling additionalProperties in JSON Schema
Object Schemas
Generate JSON Schema for REST API Request Bodies
Real-World Schemas