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.parsetakes 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
--streamparser 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'536870888In 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(), includingBuffer.concat(chunks).toString()JSON.parse(buffer), which converts the Buffer to a string firstrequire("./events.json")new TextDecoder().decode(buffer)andnew 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.json47762Search a large JSON file as a stream
events.json is the 609,375,097-byte file from the error above: one minified array of 3.2 million log records. It is that size because 609 MB is just past Node's string limit; the same commands run unchanged on a multi-gigabyte file. Install Gronify, and three commands answer most first questions about it.
gronify search events.json timeout --count47762gronify search events.json -m 3 timeoutjson[1].message = "upstream timeout after 30000 ms"json[68].message = "upstream timeout after 30000 ms"json[135].message = "upstream timeout after 30000 ms"Stopped after 3 matches (--max-count); there may be moregronify flatten --limit 12 events.jsonjson = []json[0] = {}json[0].id = 1000000json[0].ts = "2026-10-01T00:00:00.000Z"json[0].level = "info"json[0].service = "checkout"json[0].message = "request completed"json[0].request = {}json[0].request.id = "req-00000000"json[0].request.path = "/api/orders/4000"json[0].request.status = 200json[0].request.ms = 8Stopped after 12 lines (--limit); there may be more--count reads the whole file and prints the number of matching lines. -m 3 stops reading at the third match. flatten --limit 12 prints the first twelve paths, enough to learn the shape of a record. The flags are explained in the guide on how to grep JSON, and the guide to JSON for AI agents explains why the caps matter for a model's context window.
In the project's benchmark run of 5 November 2026 (gronify 1.3.1, Apple M5 Pro), counting matches in a 1 GB array of records peaked at 55 MiB of memory. fastgron 0.7.7 with its -F filter finished the same count sooner, with memory that grew with the file, so it is the better choice when you have about twice the file's size in free RAM. The full table is under benchmarks.
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 timeout47762With -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--jsonlonflattenorsearch; records stream one at a time, so memory stays flat however many lines the file has. Without the flag, a second value stops withUnexpected '{' after the top-level valueand 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.
--sortbuffers the members of one object until it closes, and spills to a temporary file in$TMPDIRpast 64 MiB. A top-level array is still written one record at a time.unflattenstreams 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
unflattenoutput builds the whole document in memory, so pass--no-colorfor 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