yq/operators/keys.md
2021-11-26 09:50:45 +11:00

1.1 KiB

Keys

Use the keys operator to return map keys or array indices.

Map keys

Given a sample.yml file of:

dog: woof
cat: meow

then

yq eval 'keys' sample.yml

will output

- dog
- cat

Array keys

Given a sample.yml file of:

- apple
- banana

then

yq eval 'keys' sample.yml

will output

- 0
- 1

Retrieve array key

Given a sample.yml file of:

- 1
- 2
- 3

then

yq eval '.[1] | key' sample.yml

will output

1

Retrieve map key

Given a sample.yml file of:

a: thing

then

yq eval '.a | key' sample.yml

will output

a

No key

Given a sample.yml file of:

{}

then

yq eval 'key' sample.yml

will output

Update map key

Given a sample.yml file of:

a:
  x: 3
  y: 4

then

yq eval '(.a.x | key) = "meow"' sample.yml

will output

a:
  meow: 3
  y: 4

Get comment from map key

Given a sample.yml file of:

a:
  # comment on key
  x: 3
  y: 4

then

yq eval '.a.x | key | headComment' sample.yml

will output

comment on key