Tools
Get Pro

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 it

Input

{"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.json

Slurp: -s

-s reads every value into a single array, so you can count, sort and group across lines.

lengthSlurpTry it

Input

{"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

4
jq -s 'length' data.json
map(.ms) | add / lengthSlurpTry it

Input

{"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.5
Average duration. jq -s 'map(.ms) | add / length' data.json

Output NDJSON: -c

-c prints each result on one line — the right format for writing JSON Lines back out.

.users[] | {id, name}CompactTry it

Input

{
  "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.json

Sum with add

add sums numbers, concatenates strings and arrays, and merges objects.

[.users[].orders[].total] | addTry it

Input

{
  "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.5
jq '[.users[].orders[].total] | add' data.json

Running state: reduce

reduce SOURCE as $x (INIT; UPDATE) walks the values and keeps one accumulator.

reduce .[] as $l ({}; .[$l.level] += 1)SlurpCompactTry it

Input

{"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}
Count by level. jq -s -c 'reduce .[] as $l ({}; .[$l.level] += 1)' data.json
reduce .items[] as $i (0; . + $i.qty * $i.price)Try it

Input

{
  "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.5
jq 'reduce .items[] as $i (0; . + $i.qty * $i.price)' data.json

Big 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

  • -s on 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.