jq reduce, slurp and NDJSON (JSON Lines)
Logs and exports often come as NDJSON: one JSON value per line. jq handles that natively — a filter runs once per value — and slurp mode lets you treat all of them as one array.
One value per line
Without flags, the filter runs on each line separately.
select(.level == "error") | .msgTry itInput
{"level":"info","msg":"started","ms":12}
{"level":"error","msg":"db timeout","ms":5003}
{"level":"info","msg":"request","ms":48}
{"level":"error","msg":"db timeout","ms":5011}Output
"db timeout"
"db timeout"jq 'select(.level == "error") | .msg' data.jsonSlurp: -s
-s reads every value into a single array, so you can count, sort and group across lines.
lengthSlurpTry itInput
{"level":"info","msg":"started","ms":12}
{"level":"error","msg":"db timeout","ms":5003}
{"level":"info","msg":"request","ms":48}
{"level":"error","msg":"db timeout","ms":5011}Output
4jq -s 'length' data.jsonmap(.ms) | add / lengthSlurpTry itInput
{"level":"info","msg":"started","ms":12}
{"level":"error","msg":"db timeout","ms":5003}
{"level":"info","msg":"request","ms":48}
{"level":"error","msg":"db timeout","ms":5011}Output
2518.5jq -s 'map(.ms) | add / length' data.jsonOutput NDJSON: -c
-c prints each result on one line — the right format for writing JSON Lines back out.
.users[] | {id, name}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
{"id":1,"name":"Ada"}
{"id":2,"name":"Linus"}
{"id":3,"name":"Grace"}jq -c '.users[] | {id, name}' data.jsonSum with add
add sums numbers, concatenates strings and arrays, and merges objects.
[.users[].orders[].total] | addTry 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
141.5jq '[.users[].orders[].total] | add' data.jsonRunning state: reduce
reduce SOURCE as $x (INIT; UPDATE) walks the values and keeps one accumulator.
reduce .[] as $l ({}; .[$l.level] += 1)SlurpCompactTry itInput
{"level":"info","msg":"started","ms":12}
{"level":"error","msg":"db timeout","ms":5003}
{"level":"info","msg":"request","ms":48}
{"level":"error","msg":"db timeout","ms":5011}Output
{"info":2,"error":2}jq -s -c 'reduce .[] as $l ({}; .[$l.level] += 1)' data.jsonreduce .items[] as $i (0; . + $i.qty * $i.price)Try 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
8.5jq 'reduce .items[] as $i (0; . + $i.qty * $i.price)' data.jsonBig files: inputs
On the command line, jq -n 'reduce inputs as $l (0; . + $l.ms)' app.log streams the lines instead of loading them all into memory like -s does.
Common mistakes
-son a single JSON array wraps it in another array:[[...]]. Only slurp when there are several values.
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.