jq select(): filter arrays by value
select(condition) passes its input through when the condition is true and drops it otherwise. Put it after .[] to keep only matching items.
Match a field value
Iterate the array, then keep items whose field equals a value.
.users[] | select(.country == "US") | .nameTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
"Grace"jq '.users[] | select(.country == "US") | .name' data.jsonBoolean fields
A boolean field is already a condition, so select(.active) is enough. Use select(.active | not) for the opposite.
.users[] | select(.active) | .nameTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
"Ada"
"Grace"jq '.users[] | select(.active) | .name' data.json.users[] | select(.active | not) | .nameTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
"Linus"jq '.users[] | select(.active | not) | .name' data.jsonCompare numbers
Use >, >=, <, <=, == and !=.
.[] | select(.age > 30) | .nameTry itInput
[
{
"name": "Ada",
"age": 36,
"team": "core"
},
{
"name": "Linus",
"age": 28,
"team": "kernel"
},
{
"name": "Grace",
"age": 45,
"team": "core"
},
{
"name": "Ken",
"age": 28,
"team": "kernel"
}
]Output
"Ada"
"Grace"jq '.[] | select(.age > 30) | .name' data.jsonSeveral conditions: and / or
Combine conditions with and and or. Parentheses make the intent clear.
.[] | select(.team == "kernel" and .age < 30) | .nameTry itInput
[
{
"name": "Ada",
"age": 36,
"team": "core"
},
{
"name": "Linus",
"age": 28,
"team": "kernel"
},
{
"name": "Grace",
"age": 45,
"team": "core"
},
{
"name": "Ken",
"age": 28,
"team": "kernel"
}
]Output
"Linus"
"Ken"jq '.[] | select(.team == "kernel" and .age < 30) | .name' data.jsonKeep the result as an array: map(select(...))
.[] | select(...) gives separate results. To get an array back — for example to count matches — use map(select(...)).
map(select(.team == "core"))CompactTry itInput
[
{
"name": "Ada",
"age": 36,
"team": "core"
},
{
"name": "Linus",
"age": 28,
"team": "kernel"
},
{
"name": "Grace",
"age": 45,
"team": "core"
},
{
"name": "Ken",
"age": 28,
"team": "kernel"
}
]Output
[{"name":"Ada","age":36,"team":"core"},{"name":"Grace","age":45,"team":"core"}]jq -c 'map(select(.team == "core"))' data.jsonmap(select(.team == "core")) | lengthTry itInput
[
{
"name": "Ada",
"age": 36,
"team": "core"
},
{
"name": "Linus",
"age": 28,
"team": "kernel"
},
{
"name": "Grace",
"age": 45,
"team": "core"
},
{
"name": "Ken",
"age": 28,
"team": "kernel"
}
]Output
2jq 'map(select(.team == "core")) | length' data.jsonMatch against a list of values
IN(...) tests whether a value is one of several.
.users[] | select(.country | IN("UK", "US")) | .nameTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
"Ada"
"Grace"jq '.users[] | select(.country | IN("UK", "US")) | .name' data.jsonItems whose array contains a value
index("x") finds a value in an array; any(...) checks whether any element matches a condition.
.users[] | select(any(.tags[]; . == "ops")) | .nameTry itInput
{
"users": [
{
"id": 1,
"name": "Ada",
"email": "ada@example.com",
"active": true,
"country": "UK",
"tags": [
"admin",
"dev"
],
"orders": [
{
"id": "o1",
"total": 30
},
{
"id": "o2",
"total": 12.5
}
]
},
{
"id": 2,
"name": "Linus",
"email": "linus@example.com",
"active": false,
"country": "FI",
"tags": [
"dev"
],
"orders": []
},
{
"id": 3,
"name": "Grace",
"email": "grace@example.com",
"active": true,
"country": "US",
"tags": [
"dev",
"ops"
],
"orders": [
{
"id": "o3",
"total": 99
}
]
}
]
}Output
"Grace"jq '.users[] | select(any(.tags[]; . == "ops")) | .name' data.jsonCommon mistakes
- Writing
select(.country = "US")— a single=is assignment. Comparison is==. - Comparing a number to a string:
"28" == 28is false. Convert withtonumberortostring.
Practice
Open the jq Playground with your own JSON — it runs the real jq 1.8 in your browser, so nothing is uploaded. The jq cheat sheet lists every filter from these lessons on one page.