Skip to content

Segment: Array Elements

William W. Kimball, Jr., MBA, MSIS edited this page Apr 28, 2021 · 6 revisions

When your YAML data contains an Array (sequence/list), select an element via its 0-based index or from the end via its negative index with -1 being the last, -2 being second-to-last, and so on. There are two forms of this type of segment selector:

  • When using dotted notation, an element selector is the element index number wrapped in square brackets: [0]
  • When using forward-slash notation, an element selector is the bare element index number: /0

A third form of this selector is available when using dotted notation but it is a less precise, implicit selection. It is identical to a Hash Key selection in that you can use the bare Array element index, like: 0. While this will work, the alternative syntax is usually preferred for its explicit precision.

For example:

- element0
- element1
- element2
- element...
- elementN

Select elementN using this YAML Path: [4] or /4

Named Arrays

YAML Path enables selecting elements of named arrays, like:

some_array:
  - element1
  - element2
  - element...
  - elementN

To select elementN from this Array, simply employ the Array's name as a prefix for the element index: some_array[3] or /some_array/3

Unlimited Depth

No matter how deeply nested your data is, YAML Path can find the data you need. For example:

-
-
  -
    - 
    -
    -
      - elementN

To select elementN from this data, use this YAML Path: [1][0][2][0] or /1/0/2/0

Clone this wiki locally