jq · lesson 8 of 32 · Filtering & conditions

select & comparisons

select(condition) lets a value through only when the condition is true — it is jq's way to filter.

Examples

Example 1Members older than 30

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(.age > 30) | .name
  1. .members[] Stream the members.
  2. select(.age > 30) Pass a member on only when the condition is true. Inside it, . is that one member.
  3. | .name Read the name of those that passed.

Result

"Ada"
"Grace"
"Alan"

Try it in the playground

Good to know

  • Comparison operators: ==, !=, <, <=, >, >=. Strings compare too: select(.role == "dev").

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.