YAML Collections

Ordered Mappings

ordered: !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58

#↓ Equivalent JSON

{
  "ordered": [
     {"Mark McGwire": 65},
     {"Sammy Sosa": 63},
     {"Ken Griffy": 58}
  ]
}

Unordered Sets

set1: !!set
  ? one
  ? two
set2: !!set {'one', "two"}

#↓ Equivalent JSON

{
  "set1": {"one": null, "two": null},
  "set2": {"one": null, "two": null}
}

Sets are represented as a Mapping where each key is associated with a null value

Nested Collections

Jack:
  id: 1
  name: Franc
  salary: 25000
  hobby:
    - a
    - b
  location: {country: "A", city: "A-A"}

#↓ Equivalent JSON

{
  "Jack": {
    "id": 1,
    "name": "Franc",
    "salary": 25000,
    "hobby": ["a", "b"],
    "location": {
        "country": "A", "city": "A-A"
    }
  }
}

Mapping of Mappings

Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
    hr: 63,
    avg: 0.288
  }

#↓ Equivalent JSON

{
  "Mark McGwire": {
    "hr": 65,
    "avg": 0.278
  },
  "Sammy Sosa": {
    "hr": 63,
    "avg": 0.288
  }
}

Sequence of Sequences

my\_sequences:
  - [1, 2, 3]
  - [4, 5, 6]
  -  
    - 7
    - 8
    - 9
    - 0 

#↓ Equivalent JSON

{
  "my\_sequences": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9, 0]
  ]
}

Sequence of Mappings

children:
  - name: Jimmy Smith
    age: 15
  - name: Jimmy Smith
    age: 15
  -
    name: Sammy Sosa
    age: 12

#↓ Equivalent JSON

{
  "children": [
    {"name": "Jimmy Smith", "age": 15},
    {"name": "Jimmy Smith", "age": 15},
    {"name": "Sammy Sosa", "age": 12}
  ]
}

Mapping to Sequences

attributes:
  - a1
  - a2
methods: [getter, setter]

#↓ Equivalent JSON

{
  "attributes": ["a1", "a2"],
  "methods": ["getter", "setter"]
}

Mapping

hr:  65       # Home runs
avg: 0.278    # Batting average
rbi: 147      # Runs Batted In

#↓ Equivalent JSON

{
  "hr": 65,
  "avg": 0.278,
  "rbi": 147
}

Sequence

- Mark McGwire
- Sammy Sosa
- Ken Griffey

#↓ Equivalent JSON

[
  "Mark McGwire",
  "Sammy Sosa",
  "Ken Griffey"
]
Comments