y
y
yq
Search…
Has
This is operation that returns true if the key exists in a map (or index in an array), false otherwise.

Has map key

Given a sample.yml file of:
1
- a: yes
2
- a: ~
3
- a:
4
- b: nope
Copied!
then
1
yq eval '.[] | has("a")' sample.yml
Copied!
will output
1
true
2
true
3
true
4
false
Copied!

Select, checking for existence of deep paths

Simply pipe in parent expressions into has
Given a sample.yml file of:
1
- a:
2
b:
3
c: cat
4
- a:
5
b:
6
d: dog
Copied!
then
1
yq eval '.[] | select(.a.b | has("c"))' sample.yml
Copied!
will output
1
a:
2
b:
3
c: cat
Copied!

Has array index

Given a sample.yml file of:
1
- []
2
- [1]
3
- [1, 2]
4
- [1, null]
5
- [1, 2, 3]
Copied!
then
1
yq eval '.[] | has(1)' sample.yml
Copied!
will output
1
false
2
false
3
true
4
true
5
true
Copied!
Last modified 2mo ago