brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: stream mode reads a huge JSON array without loading it all at once

Json
~/tips/tip-json-stream-huge-array

jq -cn --stream 'fromstream(1|truncate_stream(inputs))' big.json

Process a file that is too big for a normal jq run.

A normal jq run parses the whole document before it does anything. With a giant array that can eat all your memory. Streaming mode works through the file piece by piece.

This turns one big top level array into its elements, one per line:

jq -cn --stream 'fromstream(1|truncate_stream(inputs))' big.json

Add a normal filter after it to pick what you want:

jq -cn --stream '
  fromstream(1|truncate_stream(inputs))
  | select(.id == 2)
' big.json

In stream mode jq emits small events, each a path and a value. truncate_stream with 1 removes the outer array level, and fromstream glues the events back into whole elements. -n together with inputs makes sure the first event is not skipped. To see the raw events on a tiny file, run jq -c --stream . small.json.

Streaming saves memory and costs time, it is noticeably slower than a plain run, so I only use it when the plain run really cannot cope. The recipe assumes the data is an array at the top. If your records sit under a key such as items, the number of levels to cut is different.

Further reading: jq manual: streaming

More Json tips echo '{"id":1,"payload":"{\"user\":\"ann\",\"tags\":[\"x\",\"y\"]}"}' | jq -c '.payload | fromjson | .tags' jq empty config.json Paths() tells you where in a big JSON document a value lives jq -c --slurpfile u users.json ' const text = '{"createdAt":"2026-11-30T09:00:00Z","name":"x"}'; All Json tips (9) All topics

Link