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 an 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 herecomparison (
>=,<etc) operators hereselect operator here
or example
or exampleRunning
yq --null-input 'true or false'will output
true"yes" and "no" are strings
In the yaml 1.2 standard, support for yes/no as booleans was dropped - they are now considered strings. See '10.2.1.2. Boolean' in https://yaml.org/spec/1.2.2/
Given a sample.yml file of:
- yes
- nothen
yq '.[] | tag' sample.ymlwill output
!!str
!!strand example
and exampleRunning
yq --null-input 'true and false'will output
falseMatching nodes with select, equals and or
Given a sample.yml file of:
- a: bird
b: dog
- a: frog
b: bird
- a: cat
b: flythen
yq '[.[] | select(.a == "cat" or .b == "dog")]' sample.ymlwill output
- a: bird
b: dog
- a: cat
b: flyany returns true if any boolean in a given array is true
any returns true if any boolean in a given array is trueGiven a sample.yml file of:
- false
- truethen
yq 'any' sample.ymlwill output
trueany returns false for an empty array
any returns false for an empty arrayGiven a sample.yml file of:
[]then
yq 'any' sample.ymlwill output
falseany_c returns true if any element in the array is true for the given condition.
any_c returns true if any element in the array is true for the given condition.Given a sample.yml file of:
a:
- rad
- awesome
b:
- meh
- whateverthen
yq '.[] |= any_c(. == "awesome")' sample.ymlwill output
a: true
b: falseall returns true if all booleans in a given array are true
all returns true if all booleans in a given array are trueGiven a sample.yml file of:
- true
- truethen
yq 'all' sample.ymlwill output
trueall returns true for an empty array
all returns true for an empty arrayGiven a sample.yml file of:
[]then
yq 'all' sample.ymlwill output
trueall_c returns true if all elements in the array are true for the given condition.
all_c returns true if all elements in the array are true for the given condition.Given a sample.yml file of:
a:
- rad
- awesome
b:
- meh
- 12then
yq '.[] |= all_c(tag == "!!str")' sample.ymlwill output
a: true
b: falseNot true is false
Running
yq --null-input 'true | not'will output
falseNot false is true
Running
yq --null-input 'false | not'will output
trueString values considered to be true
Running
yq --null-input '"cat" | not'will output
falseEmpty string value considered to be true
Running
yq --null-input '"" | not'will output
falseNumbers are considered to be true
Running
yq --null-input '1 | not'will output
falseZero is considered to be true
Running
yq --null-input '0 | not'will output
falseNull is considered to be false
Running
yq --null-input '~ | not'will output
trueLast updated
Was this helpful?