y
y
yq
Search…
Keys
Use the keys operator to return map keys or array indices.

Map keys

Given a sample.yml file of:
1
dog: woof
2
cat: meow
Copied!
then
1
yq eval '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 eval '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 eval '.[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 eval '.a | key' sample.yml
Copied!
will output
1
a
Copied!

No key

Given a sample.yml file of:
1
{}
Copied!
then
1
yq eval '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 eval '(.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 eval '.a.x | key | headComment' sample.yml
Copied!
will output
1
comment on key
Copied!
Last modified 1mo ago