Entries
Similar to the same named functions in
jq
these functions convert to/from an object and an array of key-value pairs. This is most useful for performing operations on keys of maps.Given a sample.yml file of:
a: 1
b: 2
then
yq 'to_entries' sample.yml
will output
- key: a
value: 1
- key: b
value: 2
Given a sample.yml file of:
- a
- b
then
yq 'to_entries' sample.yml
will output
- key: 0
value: a
- key: 1
value: b
Given a sample.yml file of:
null
then
yq 'to_entries' sample.yml
will output
Given a sample.yml file of:
a: 1
b: 2
then
yq 'to_entries | from_entries' sample.yml
will output
a: 1
b: 2
from_entries always creates a map, even for numeric keys
Given a sample.yml file of:
- a
- b
then
yq 'to_entries | from_entries' sample.yml
will output
0: a
1: b
Given a sample.yml file of:
a: 1
b: 2
then
yq 'with_entries(.key |= "KEY_" + .)' sample.yml
will output
KEY_a: 1
KEY_b: 2
Use to_entries to convert to an array of key/value pairs, sort the array using sort/sort_by/etc, and convert it back.
Given a sample.yml file of:
a: 1
c: 3
b: 2
then
yq 'to_entries | sort_by(.key) | reverse | from_entries' sample.yml
will output
c: 3
b: 2
a: 1
Given a sample.yml file of:
a:
b: bird
c:
d: dog
then
yq 'with_entries(select(.value | has("b")))' sample.yml
will output
a:
b: bird
Last modified 6mo ago