A list of objects is a list whose items each hold several keys, such as books with a title and a price. A dash starts each object, and the rest of its keys line up under its first key.
Every command and output on this page is real, run with yq 4.54.3 on macOS.
Syntax
Syntax
key:
- field: value
other_field: value
- field: value
other_field: value
- field: value: a dash and a space start a new object, with its first key on the same line.other_field: value: the object’s next key, lined up underfield, not under the dash.- Each new dash starts the next object.
Each object is a mapping inside a list: see Block Sequences in the YAML 1.2.2 specification.
Prerequisites
- yq, to check the YAML. Install it from yq’s install instructions.
The Example
Two books, each with a title and a price, in a file named books.yml.
Step 1: Write the YAML
books.yml
books:
- title: Salt and Starlight
price: 12
- title: A Map of Quiet Places
price: 15
Step 2: Check It with yq
yq reads the file and prints it back:
Terminal
yq '.' books.yml
Output
books:
- title: Salt and Starlight
price: 12
- title: A Map of Quiet Places
price: 15
Step 3: Read a Value from One Object
Terminal
yq '.books[1].title' books.yml
Output
A Map of Quiet Places
[1] picks the second book, because yq counts from 0, and .title picks its title.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
The second key lined up with the dash: price under - | yq stops: Error: bad file 'books.yml': go-yaml load error in parser (while parsing a block collection) at L2.C3-L3.C3: did not find expected '-' indicator | Line up price under title |
A dash before every key: - title: Salt and Starlight, then - price: 12 | No error, but each key becomes its own object: yq '.books[0]' books.yml prints only title: Salt and Starlight | Use one dash per object |
FAQ
Is a list of objects the same as an array of objects in JSON?
Yes, it’s the same structure. YAML calls the list a sequence and each object a mapping.
Conclusion
Start each object with a dash and a space, put its first key on the same line, and line up its other keys under the first one.
Additional Resources
- Block Sequences (YAML 1.2.2 specification)
- Block Mappings (YAML 1.2.2 specification)
- Traverse (Read) (yq documentation)
![Featured card for How to Write a List of Objects in YAML: a books.yml file with two books, Salt and Starlight at 12 and A Map of Quiet Places at 15, and a yq command that reads .books[1].title and prints A Map of Quiet Places.](https://i0.wp.com/jd-bots.com/wp-content/uploads/2026/11/write-list-objects-yaml-featured.webp?fit=728%2C410&ssl=1)