yq/pkg/yqlib/doc/operators/array-to-map.md
2022-11-13 11:16:01 +11:00

458 B

Array to Map

Use this operator to convert an array to..a map. The indices are used as map keys, null values in the array are skipped over.

Behind the scenes, this is implemented using reduce:

(.[] | select(. != null) ) as $i ireduce({}; .[$i | key] = $i)

Simple example

Given a sample.yml file of:

cool:
  - null
  - null
  - hello

then

yq '.cool |= array_to_map' sample.yml

will output

cool:
  2: hello