YAML by Example: Nested Structures
1.2
Combine lists and maps. Create complex hierarchies.
Code
invoice: 34843
date: 2001-01-23
bill-to: &id001
given: Chris
family: Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city: Royal Oak
state: MI
postal: 48046
ship-to: *id001
product:
- sku: BL394D
quantity: 4
description: Basketball
price: 450.00
- sku: BL4438H
quantity: 1
description: Super Hoop
price: 2392.00Explanation
YAML handles arbitrary nesting of sequences and mappings. You can have lists of objects, objects containing lists, or any combination. Indentation defines the hierarchy, making complex data structures readable.
This example shows a typical invoice structure with nested address details and a list of product items. The anchor on billing address is aliased for shipping, demonstrating how anchors work within nested structures to avoid duplication.
Code Breakdown
6
address: starts a nested mapping. Indentation increases.13
product: starts a list of objects.
