jq · lesson 9 of 32 · Filtering & conditions
and / or / not
Combine conditions with and and or, and invert one with not.
Examples
Example 1Active devs or QA
Document
{
"team": "Platform",
"office": { "city": "Berlin", "floor": 3 },
"members": [
{ "name": "Ada", "age": 36, "role": "lead", "active": true,
"email": "[email protected]", "skills": ["rust", "sql"] },
{ "name": "Linus", "age": 28, "role": "dev", "active": true,
"email": null, "skills": ["c", "shell"] },
{ "name": "Grace", "age": 45, "role": "dev", "active": false,
"skills": ["cobol"] },
{ "name": "Alan", "age": 41, "role": "qa", "active": true,
"email": "[email protected]", "skills": [] }
]
}Query
.members[] | select((.role == "dev" or .role == "qa") and .active) | .name
(.role == "dev" or .role == "qa")Parentheses group theor, which is true when either side is: the role isdevorqa.andBoth sides must be true..activeA boolean field works as a condition on its own: the member is active.
Result
"Linus" "Alan"
Example 2Members who are not active
Document
{
"team": "Platform",
"office": { "city": "Berlin", "floor": 3 },
"members": [
{ "name": "Ada", "age": 36, "role": "lead", "active": true,
"email": "[email protected]", "skills": ["rust", "sql"] },
{ "name": "Linus", "age": 28, "role": "dev", "active": true,
"email": null, "skills": ["c", "shell"] },
{ "name": "Grace", "age": 45, "role": "dev", "active": false,
"skills": ["cobol"] },
{ "name": "Alan", "age": 41, "role": "qa", "active": true,
"email": "[email protected]", "skills": [] }
]
}Query
.members[] | select(.active | not) | .name
select(Keep only what the condition accepts….active | not…andnotflips a boolean. It is a filter, so it is piped:.active | not.
Result
"Grace"
Good to know
- Only
falseandnullcount as false; everything else — even0and""— is true.