y
y
yq
Search…
Keys
Use the keys operator to return map keys or array indices.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Map keys

Given a sample.yml file of:
1
dog: woof
2
cat: meow
Copied!
then
1
yq 'keys' sample.yml
Copied!
will output
1
- dog
2
- cat
Copied!

Array keys

Given a sample.yml file of:
1
- apple
2
- banana
Copied!
then
1
yq 'keys' sample.yml
Copied!
will output
1
- 0
2
- 1
Copied!

Retrieve array key

Given a sample.yml file of:
1
- 1
2
- 2
3
- 3
Copied!
then
1
yq '.[1] | key' sample.yml
Copied!
will output
1
1
Copied!

Retrieve map key

Given a sample.yml file of:
1
a: thing
Copied!
then
1
yq '.a | key' sample.yml
Copied!
will output
1
a
Copied!

No key

Given a sample.yml file of:
1
{}
Copied!
then
1
yq 'key' sample.yml
Copied!
will output
1
Copied!

Update map key

Given a sample.yml file of:
1
a:
2
x: 3
3
y: 4
Copied!
then
1
yq '(.a.x | key) = "meow"' sample.yml
Copied!
will output
1
a:
2
meow: 3
3
y: 4
Copied!

Get comment from map key

Given a sample.yml file of:
1
a:
2
# comment on key
3
x: 3
4
y: 4
Copied!
then
1
yq '.a.x | key | headComment' sample.yml
Copied!
will output
1
comment on key
Copied!
Last modified 4mo ago