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
  1. (.role == "dev" or .role == "qa") Parentheses group the or, which is true when either side is: the role is dev or qa.
  2. and Both sides must be true.
  3. .active A boolean field works as a condition on its own: the member is active.

Result

"Linus"
"Alan"

Try it in the playground

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
  1. select( Keep only what the condition accepts…
  2. .active | not …and not flips a boolean. It is a filter, so it is piped: .active | not.

Result

"Grace"

Try it in the playground

Good to know

  • Only false and null count as false; everything else — even 0 and "" — is true.

About jsonquery

Query, format and learn JSON with jq, JSONPath, JMESPath and JSON Pointer. It is the engine of the desktop app, run in your browser.

Engine
The desktop app’s, compiled to WebAssembly
Licence
MIT (the engine and the app)
Source
GitHub
Contact
The contact form (private)
Public issues
GitHub
Legal
Privacy policy

Known limitations: a file over 10 MB opens read-only and one over 512 MB does not open here; the desktop app has no such limit.