Skip to content

YAML Sequences and Mappings Combinations

Explore practical examples of combining YAML sequences and mappings, including mapping-to-sequence, sequence-of-mappings, sequence-of-sequences, and mapping-of-mappings with JSON equivalents.

YAML Sequences and Mappings Combinations

Some examples of YAML sequences and mappings combined together:

1. Mapping to Sequences

attributes:
  - attr1
  - attr2
methods: [getter, setter]
Equivalent JSONjson
{
  "attributes": ["attr1", "attr2"],
  "methods": ["getter", "setter"]
}

2. Sequence-of-Mappings

friends:
  - name: Tom Nolan
    age: 23
  - name: Ryan Raynolds
    age: 45
  -
    name: Thomas Anderson
    age: 28
Equivalent JSONjson
{
  "friends": [
    {"name": "Tom Nolan", "age": 23},
    {"name": "Ryan Raynolds", "age": 45},
    {"name": "Thomas Anderson", "age": 28}
  ]
}

3. Sequence-of-Sequences

my_sequences:
  - [1, 2, 3]
  - [4, 5, 6]
  -  
    - 7
    - 8
    - 9
    - 10
Equivalent JSONjson
{
  "my_sequences": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9, 10]
  ]
}

4. Mapping-of-Mappings

Tom Nolan: {score: 65, avg: 0.278}
Ryan Raynolds: {
    score: 63,
    avg: 0.288
  }
Equivalent JSONjson
{
  "Tom Nolan": {
    "score": 65,
    "avg": 0.278
  },
  "Ryan Raynolds": {
    "score": 63,
    "avg": 0.288
  }
}
Navigation