y
y
yq
Search…
Boolean Operators
The or and and operators take two parameters and return a boolean result.
not flips a boolean from true to false, or vice versa.
any will return true if there are any true values in a array sequence, and all will return true if all elements in an array are true.
any_c(condition) and all_c(condition) are like any and all but they take a condition expression that is used against each element to determine if it's true. Note: in jq you can simply pass a condition to any or all and it simply works - yq isn't that clever..yet
These are most commonly used with the select operator to filter particular nodes.

Related Operators

  • equals / not equals (==, !=) operators here
  • comparison (>=, < etc) operators here
  • select operator here
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

or example

Running
1
yq --null-input 'true or false'
Copied!
will output
1
true
Copied!

and example

Running
1
yq --null-input 'true and false'
Copied!
will output
1
false
Copied!

Matching nodes with select, equals and or

Given a sample.yml file of:
1
- a: bird
2
b: dog
3
- a: frog
4
b: bird
5
- a: cat
6
b: fly
Copied!
then
1
yq '[.[] | select(.a == "cat" or .b == "dog")]' sample.yml
Copied!
will output
1
- a: bird
2
b: dog
3
- a: cat
4
b: fly
Copied!

any returns true if any boolean in a given array is true

Given a sample.yml file of:
1
- false
2
- true
Copied!
then
1
yq 'any' sample.yml
Copied!
will output
1
true
Copied!

any returns false for an empty array

Given a sample.yml file of:
1
[]
Copied!
then
1
yq 'any' sample.yml
Copied!
will output
1
false
Copied!

any_c returns true if any element in the array is true for the given condition.

Given a sample.yml file of:
1
a:
2
- rad
3
- awesome
4
b:
5
- meh
6
- whatever
Copied!
then
1
yq '.[] |= any_c(. == "awesome")' sample.yml
Copied!
will output
1
a: true
2
b: false
Copied!

all returns true if all booleans in a given array are true

Given a sample.yml file of:
1
- true
2
- true
Copied!
then
1
yq 'all' sample.yml
Copied!
will output
1
true
Copied!

all returns true for an empty array

Given a sample.yml file of:
1
[]
Copied!
then
1
yq 'all' sample.yml
Copied!
will output
1
true
Copied!

all_c returns true if all elements in the array are true for the given condition.

Given a sample.yml file of:
1
a:
2
- rad
3
- awesome
4
b:
5
- meh
6
- 12
Copied!
then
1
yq '.[] |= all_c(tag == "!!str")' sample.yml
Copied!
will output
1
a: true
2
b: false
Copied!

Not true is false

Running
1
yq --null-input 'true | not'
Copied!
will output
1
false
Copied!

Not false is true

Running
1
yq --null-input 'false | not'
Copied!
will output
1
true
Copied!

String values considered to be true

Running
1
yq --null-input '"cat" | not'
Copied!
will output
1
false
Copied!

Empty string value considered to be true

Running
1
yq --null-input '"" | not'
Copied!
will output
1
false
Copied!

Numbers are considered to be true

Running
1
yq --null-input '1 | not'
Copied!
will output
1
false
Copied!

Zero is considered to be true

Running
1
yq --null-input '0 | not'
Copied!
will output
1
false
Copied!

Null is considered to be false

Running
1
yq --null-input '~ | not'
Copied!
will output
1
true
Copied!