y
y
yq
Search…
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.

to_entries Map

Given a sample.yml file of:
1
a: 1
2
b: 2
Copied!
then
1
yq eval 'to_entries' sample.yml
Copied!
will output
1
- key: a
2
value: 1
3
- key: b
4
value: 2
Copied!

to_entries Array

Given a sample.yml file of:
1
- a
2
- b
Copied!
then
1
yq eval 'to_entries' sample.yml
Copied!
will output
1
- key: 0
2
value: a
3
- key: 1
4
value: b
Copied!

to_entries null

Given a sample.yml file of:
1
null
Copied!
then
1
yq eval 'to_entries' sample.yml
Copied!
will output
1
Copied!

from_entries map

Given a sample.yml file of:
1
a: 1
2
b: 2
Copied!
then
1
yq eval 'to_entries | from_entries' sample.yml
Copied!
will output
1
a: 1
2
b: 2
Copied!

from_entries with numeric key indexes

from_entries always creates a map, even for numeric keys
Given a sample.yml file of:
1
- a
2
- b
Copied!
then
1
yq eval 'to_entries | from_entries' sample.yml
Copied!
will output
1
0: a
2
1: b
Copied!

Use with_entries to update keys

Given a sample.yml file of:
1
a: 1
2
b: 2
Copied!
then
1
yq eval 'with_entries(.key |= "KEY_" + .)' sample.yml
Copied!
will output
1
KEY_a: 1
2
KEY_b: 2
Copied!

Use with_entries to filter the map

Given a sample.yml file of:
1
a:
2
b: bird
3
c:
4
d: dog
Copied!
then
1
yq eval 'with_entries(select(.value | has("b")))' sample.yml
Copied!
will output
1
a:
2
b: bird
Copied!
Last modified 2mo ago