Skip to content

Segment: Array of Hashes Pass Through Selection

William W. Kimball, Jr., MBA, MSIS edited this page Mar 3, 2020 · 2 revisions

One of YAML Path's more novel capabilities, it can return the value of one key from each Hash in an Array of Hashes. Called a "pass-through" selection, this is expressed by indicating the desired sub key name from the parent Hash, omitting an element reference. For example:

warriors:
  - name: Chichi Shiranui
    power_level: 5280
    style: Dragon fury
  - name: Goku Higashi
    power_level: 9001
    style: Z fight
  - name: Krillin Bogard
    power_level: 5280
    style: Fatal ball
  - name: Bulma Sakazaki
    power_level: 1024
    style: Super final

Users could simply return the power_level of all warriors using warriors.power_level or /warriors/power_level. This gives one result per warrior:

5280
9001
5280
1024

Contrast this with a segment specified using an element index; warriors[1].power_level or /warriors[1]/power_level would give just one result:

9001
Clone this wiki locally