Skip to content

Segment: Array Elements

William W. Kimball, Jr., MBA, MSIS edited this page Mar 1, 2020 · 6 revisions

When your YAML data contains an array, select an element via its 0-based element index. There are two forms of this type of segment selector:

  • When using dot 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 dot-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 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