Keys
Use the keys operator to return map keys or array indices.
Map keys
Given a sample.yml file of:
dog: woof
cat: meowthen
yq 'keys' sample.ymlwill output
- dog
- catArray keys
Given a sample.yml file of:
- apple
- bananathen
yq 'keys' sample.ymlwill output
- 0
- 1Retrieve array key
Given a sample.yml file of:
- 1
- 2
- 3then
yq '.[1] | key' sample.ymlwill output
1Retrieve map key
Given a sample.yml file of:
a: thingthen
yq '.a | key' sample.ymlwill output
aNo key
Given a sample.yml file of:
{}then
yq 'key' sample.ymlwill output
Update map key
Given a sample.yml file of:
a:
x: 3
y: 4then
yq '(.a.x | key) = "meow"' sample.ymlwill output
a:
meow: 3
y: 4Get comment from map key
Given a sample.yml file of:
a:
# comment on key
x: 3
y: 4then
yq '.a.x | key | headComment' sample.ymlwill output
comment on keyCheck node is a key
Given a sample.yml file of:
a:
b:
- cat
c: frogthen
yq '[... | { "p": path | join("."), "isKey": is_key, "tag": tag }]' sample.ymlwill output
- p: ""
isKey: false
tag: '!!map'
- p: a
isKey: true
tag: '!!str'
- p: a
isKey: false
tag: '!!map'
- p: a.b
isKey: true
tag: '!!str'
- p: a.b
isKey: false
tag: '!!seq'
- p: a.b.0
isKey: false
tag: '!!str'
- p: a.c
isKey: true
tag: '!!str'
- p: a.c
isKey: false
tag: '!!str'Last updated
Was this helpful?