jq variables: as $x, --arg and $ENV
Variables let you keep a value around while the pipe moves on — and pass values in from the shell without string-building your filter.
Bind a value: expr as $name
The variable is visible in everything after the |.
.id as $order | .items[] | {order: $order, sku}CompactTry itInput
{
"id": "A-1001",
"customer": {
"name": "Ada Lovelace",
"address": {
"city": "London",
"zip": "NW1"
}
},
"items": [
{
"sku": "pen",
"qty": 3,
"price": 1.5
},
{
"sku": "notebook",
"qty": 1,
"price": 4
}
],
"status": "paid",
"created at": "2026-11-01T10:00:00Z"
}Output
{"order":"A-1001","sku":"pen"}
{"order":"A-1001","sku":"notebook"}jq -c '.id as $order | .items[] | {order: $order, sku}' data.jsonUse the outer value inside a loop
Typical use: compare every item with something computed from the whole input.
(map(.age) | add / length) as $avg | map(select(.age > $avg) | .name)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
["Ada","Grace"]jq -c '(map(.age) | add / length) as $avg | map(select(.age > $avg) | .name)' data.jsonPass values from the shell: --arg
--arg name value creates $name as a string. Never paste shell variables into the filter text — quotes in the value would break it.
jq --arg country "$COUNTRY" '.users[] | select(.country == $country) | .name' users.json.users[] | select(.country == $country) | .nameInput
{
"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"jq --arg country UK '.users[] | select(.country == $country) | .name' data.jsonNumbers and JSON: --argjson
--arg always gives a string. Use --argjson for numbers, booleans or objects.
map(select(.age >= $min) | .name)CompactInput
[
{
"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 -c --argjson min 36 'map(select(.age >= $min) | .name)' data.jsonEnvironment variables: $ENV
REGION=eu jq -n '$ENV.REGION'Define a function: def
def spent: [.orders[].total] | add // 0; .users | map({name, spent: spent})CompactTry 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
[{"name":"Ada","spent":42.5},{"name":"Linus","spent":0},{"name":"Grace","spent":99}]jq -c 'def spent: [.orders[].total] | add // 0; .users | map({name, spent: spent})' data.jsonCommon mistakes
- Comparing
--argvalues with numbers fails silently:"36" == 36is false. Use--argjsonortonumber.
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.