Skip to main content

Search a multi-gigabyte JSON file from the command line

To search a JSON file that is too large to open, read it as a stream instead of loading it. gronify search big.json timeout --count tokenizes the file in chunks and tests each json.path = value line as it is produced, so memory does not grow with the number of records.

Updated

Why JSON.parse, jq, and editors fail on large JSON files

The usual tools read the whole document before they do any work, so their memory grows with the file.

  • JSON.parse takes a string. The whole file must become one JavaScript string, and then the parser builds every object and array before it returns. In Node.js the first step fails on a file over about 512 MB.
  • jq does not stream by default. Its FAQ describes the --stream parser as the way "to avoid having to read JSON texts completely before processing them". Without it, jq parses each JSON text completely before the filter runs, and a file that is one large array is one text.
  • gron 0.7.1 decodes the whole document before it prints, and so does any editor or browser viewer that loads the file before it shows anything.

What "Cannot create a string longer than 0x1fffffe8 characters" means

Node.js throws this error when one operation would produce a JavaScript string longer than the engine allows. The limit is on the length of one string, so free RAM does not change it. Here it is on Node v22.23.1 with a 609 MB file:

wc -c events.json 609375097 events.jsonnode -e 'JSON.parse(require("fs").readFileSync("events.json", "utf8"))' 2>&1 | grep -E "Error|code"Error: Cannot create a string longer than 0x1fffffe8 characters code: 'ERR_STRING_TOO_LONG'

What the number is

0x1fffffe8 is hexadecimal for 536,870,888, or 2^29 minus 24. That is the longest string V8, the JavaScript engine inside Node, will create, counted in UTF-16 code units. Node exposes it as buffer.constants.MAX_STRING_LENGTH:

node -p 'require("buffer").constants.MAX_STRING_LENGTH'536870888

In ASCII JSON one byte becomes one code unit, so the ceiling is a file of about 512 MB. The Node.js documentation says the value "may depend on the JS engine that is being used", so print the constant on your own version before you rely on 512 MB.

What triggers it

Any call that turns more than that many bytes into one string triggers it. On Node v22.23.1, each of these failed with ERR_STRING_TOO_LONG on the 609 MB file:

  • fs.readFileSync(path, "utf8")
  • buffer.toString(), including Buffer.concat(chunks).toString()
  • JSON.parse(buffer), which converts the Buffer to a string first
  • require("./events.json")
  • new TextDecoder().decode(buffer) and new Response(buffer).text()

The same limit has a second message. When JavaScript code builds the string, V8 throws RangeError: Invalid string length, with no code property. fs.promises.readFile(path, "utf8"), data += chunk in a stream handler, and chunks.join("") all ended that way in the same test.

Why --max-old-space-size does not help

--max-old-space-size raises the ceiling of V8's heap. The string limit is a constant of the engine, so a 16 GB heap fails the same way:

node --max-old-space-size=16384 -e 'JSON.parse(require("fs").readFileSync("events.json", "utf8"))' 2>&1 | grep -E "Error|code"Error: Cannot create a string longer than 0x1fffffe8 characters code: 'ERR_STRING_TOO_LONG'

The flag fixes a different failure. A 300 MB file fits in one string, but with a 512 MB heap JSON.parse died with FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory. With --max-old-space-size=8192 the same file parsed.

The 2 GiB limit above it

Reading a file larger than 2 GiB in one call, with no encoding, fails with ERR_FS_FILE_TOO_LARGE. On a 3 GiB file, Node v22.23.1 reported File size (3221225472) is greater than 2 GiB. So between 512 MB and 2 GiB a file fits in a Buffer but cannot become a string, and above 2 GiB it cannot be read in one call.

How to fix it

Stop creating the string. Fixing ERR_STRING_TOO_LONG in Node.js shows one streaming reader and one streaming writer that avoid the error. In short:

  • In a program, pipe fs.createReadStream() into a streaming parser such as stream-json or @streamparser/json, which hands your code one record at a time.
  • If you control the writer, write JSON Lines, one object per line, so each line is a small string.
  • If you only need to look inside the file, use a streaming command-line tool, as in the sections below.

This script uses stream-json 3.9.0 to count the error records in the file that JSON.parse could not read:

cat count-errors.mjsimport fs from "node:fs";import chain from "stream-chain";import { parser } from "stream-json";import { streamArray } from "stream-json/streamers/stream-array.js";let errors = 0;const records = chain([ fs.createReadStream(process.argv[2]), parser(), streamArray(),]);records.on("data", ({ value }) => { if (value.level === "error") errors += 1;});records.on("end", () => console.log(errors));node count-errors.mjs events.json47762

Search gzip and zstd files without unpacking them

Gronify has no decompressor of its own. Leave out the file name and it reads standard input, so the decompressor streams into it and nothing is unpacked to disk.

gzip -dc events.json.gz | gronify search -m 2 "status = 504"json[1].request.status = 504json[68].request.status = 504Stopped after 2 matches (--max-count); there may be morezstd -dc events.json.zst | gronify search --count timeout47762

With -m, Gronify closes the pipe once it has its matches, which kills the decompressor. In the first command gzip exited with status 141 (SIGPIPE) and Gronify with 0, so under set -o pipefail the pipeline reports 141 for a search that succeeded.

What jq --stream looks like for the same search

With --stream, a jq program no longer receives the document. It receives one [path, leaf] array per scalar, plus a one-element [path] event each time an object or array closes. small.json holds the first three records of the large file. These are the events for the first record in jq 1.8.2:

jq -c --stream . small.json | head -11[[0,"id"],1000000][[0,"ts"],"2026-10-01T00:00:00.000Z"][[0,"level"],"info"][[0,"service"],"checkout"][[0,"message"],"request completed"][[0,"request","id"],"req-00000000"][[0,"request","path"],"/api/orders/4000"][[0,"request","status"],200][[0,"request","ms"],8][[0,"request","ms"]][[0,"request"]]

Each event carries the same information as one flattened line, so a search is a select on the leaf:

jq -c --stream 'select(.[1] == "error")' small.json[[1,"level"],"error"]gronify search small.json errorjson[1].level = "error"

jq --stream is still the right tool when the answer needs jq's language on a file too large for its normal mode, such as selecting whole records by a condition or reshaping them. The idiom from the jq FAQ rebuilds one top-level array element at a time and passes it to an ordinary filter:

jq -cn --stream 'fromstream(1 | truncate_stream(inputs)) | select(.level == "error") | {id, path: .request.path}' small.json{"id":1000001,"path":"/api/cart/4001"}

The same command ran unchanged on the 609 MB file. A line filter sees one value at a time, so Gronify cannot do this. The jq alternative guide covers how the two tools divide the work.

Limits of Gronify on large files

Streaming removes the dependence on record count. These limits remain:

  • One JSON document per run by default. For JSON Lines / NDJSON (one JSON value per line), pass -s, --stream, or --jsonl on flatten or search; records stream one at a time, so memory stays flat however many lines the file has. Without the flag, a second value stops with Unexpected '{' after the top-level value and a tip to pass --stream.
  • Memory follows the largest single token and the nesting depth. One string or number must fit in memory, and the output of a very deep document grows with the square of the depth.
  • --sort buffers the members of one object until it closes, and spills to a temporary file in $TMPDIR past 64 MiB. A top-level array is still written one record at a time.
  • unflatten streams only input in document order. Out-of-order lines within the first 16 MiB make it rebuild the whole document in memory. After that they are an error.
  • Colored unflatten output builds the whole document in memory, so pass --no-color for large inputs.
  • Errors can arrive late. Output is written while the input is read, so a truncated file prints lines before the error. Check the exit status.

Use this workflow with a coding agent

To teach Claude Code, Cursor, Codex, or another supported agent to check file size first and stream large JSON with --limit, --count, and -m, install the large-json-search skill (see JSON exploration for AI coding agents):

npx skills add 1solomonwakhungu/gronify --skill large-json-search

Frequently asked questions

How do I look at the first record of a huge JSON array?

Flatten the first few lines and stop: gronify flatten --limit 12 events.json printed the first record of the 609 MB file above without reading the rest. To get that record back as JSON, pipe the same lines into gronify unflatten, which closes the open array for you. The limit has to end on a record boundary, or the output includes part of the next record.

Gronify runs on Node.js. Why does it not hit the string length limit?

It never builds the string. flatten and search read the input in chunks (1 MiB from a file path, 64 KiB from standard input), tokenize the raw bytes, and write each line as it is produced. No step holds the file as one JavaScript string, so neither the 512 MB string limit nor the 2 GiB read limit applies.

Does --count count records or matching lines?

Lines. A record with the term in two values counts twice. To count records, match one key that appears once per record: gronify search events.json --count 'level = "error"' printed 47762 on the file above, one line per error record.

How do I split a large JSON array into smaller files?

Stream the elements out as JSON Lines and split on line count: jq -cn --stream 'fromstream(1 | truncate_stream(inputs))' events.json | split -l 1000000 - part- wrote four files from the 3.2 million records above, each line one complete record. Gronify does not write JSON Lines, so this is a job for jq.

Try it on your own JSON.

Gronify is open source and MIT licensed. Install it with npm or Homebrew, or read the source first.

Install GronifyView the repository