Filtering

Filter JSON with jq select() (by Value or Condition)

3 min read

select(condition) passes through its input only when the condition is true — the core of filtering an array of objects.

Example input

[
  { "name": "Ada",   "age": 36, "active": true  },
  { "name": "Linus", "age": 54, "active": false },
  { "name": "Mae",   "age": 28, "active": true  }
]

Filter by a numeric condition

jq '.[] | select(.age > 30)' data.json

Output:

{ "name": "Ada", "age": 36, "active": true }
{ "name": "Linus", "age": 54, "active": false }

Re-collect the matches into an array

jq '[.[] | select(.age > 30)]' data.json

Filter by string equality

jq '.[] | select(.name == "Ada")' data.json

Filter by a boolean

jq '.[] | select(.active)' data.json

Combine conditions (AND / OR)

jq '.[] | select(.active and .age < 40)' data.json     # AND
jq '.[] | select(.age < 30 or .age > 50)' data.json    # OR

Just the matching names

jq '[.[] | select(.active) | .name]' data.json     # ["Ada","Mae"]

map(select(...)) is the same thing: map(select(.active)) equals [.[] | select(.active)] — both keep matching elements in an array. Use select inside .[] | … when you’ll pipe each match onward, and the array form when you want the filtered list back.

Open the full version (with copy buttons) ↗

← All recipes