yq/operators/has.md
2022-02-06 14:41:27 +11:00

933 B

Has

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

{% hint style="warning" %} Note that versions prior to 4.18 require the 'eval/e' command to be specified.

yq e <exp> <file> {% endhint %}

Has map key

Given a sample.yml file of:

- a: yes
- a: ~
- a:
- b: nope

then

yq '.[] | has("a")' sample.yml

will output

true
true
true
false

Select, checking for existence of deep paths

Simply pipe in parent expressions into has

Given a sample.yml file of:

- a:
    b:
      c: cat
- a:
    b:
      d: dog

then

yq '.[] | select(.a.b | has("c"))' sample.yml

will output

a:
  b:
    c: cat

Has array index

Given a sample.yml file of:

- []
- [1]
- [1, 2]
- [1, null]
- [1, 2, 3]

then

yq '.[] | has(1)' sample.yml

will output

false
false
true
true
true