Skip to content

YAML – Block Sequences

Block sequences are the most common way to represent lists or arrays in YAML. They are characterized by each item starting with a dash and a space (- ) on a new line, with all items in the sequence sharing the same indentation level.

The basic structure for a block sequence item is:

- item_value

If a sequence is a value within a mapping, it’s indented relative to its parent key:

parent_key:
- item1
- item2
- another_item: value # Items can be complex structures

A straightforward list of programming languages:

languages:
- Python
- JavaScript
- Ruby
- Go

This YAML defines a key languages whose value is a sequence of four strings. Conceptually, this would be parsed into a structure like:

{
"languages": [
"Python",
"JavaScript",
"Ruby",
"Go"
]
}

Block sequences can also contain more complex items, such as mappings (objects). This is common for lists of records or configurations.

users:
- name: Alice
role: admin
id: 101
- name: Bob
role: editor
id: 102
- name: Charlie
role: viewer
id: 103

Here, users is a sequence where each item is a mapping with name, role, and id keys. The JSON equivalent would be:

{
"users": [
{
"name": "Alice",
"role": "admin",
"id": 101
},
{
"name": "Bob",
"role": "editor",
"id": 102
},
{
"name": "Charlie",
"role": "viewer",
"id": 103
}
]
}

Important Considerations for Block Sequences:

Section titled “Important Considerations for Block Sequences:”
  • Indentation: The dash - is considered part of the indentation. All items in the same block sequence must have their dashes aligned at the same indentation level.
  • Whitespace: A space is required after the dash (- ) before the item’s content.
  • Nested Structures: Items within a block sequence can themselves be scalars, mappings, or other sequences, allowing for deeply nested data structures.

Block sequences are fundamental to YAML’s readability and are widely used for representing ordered collections of data.