How to grep nested JSON from the command line
To grep JSON, flatten it first so that every value sits on its own line next to its full path, then filter those lines. gronify search orders.json pending does both in one streaming pass and prints each match as json.path = value, so you can see where in the document the match lives.
Updated
Why plain grep fails on JSON
grep works one line at a time, and JSON does not put one fact on one line. APIs and export jobs usually write it minified, so the whole document is a single line and any hit prints all of it. The examples on this page use a small orders payload saved as orders.json:
grep pending orders.json{"service":{"name":"checkout","status":"degraded"},"orders":[{"id":1001,"status":"shipped","customer":{"email":"ada@example.com"}},{"id":1002,"status":"Pending","customer":{"email":"grace@example.com"}},{"id":1003,"status":"pending","customer":{"email":"alan@example.org"},"error":{"code":"CARD_DECLINED"}}]}Pretty-printing first gives grep one key per line, and then the matching line has lost its parents:
jq . orders.json | grep pending "status": "pending",That output says some status is pending. It does not say which order, or whether the key sits under orders at all. It also skipped order 1002, whose status is spelled Pending, because grep is case-sensitive unless you pass -i.
Flatten first, then filter
gron solved this by rewriting the document as one assignment per value, each with its full path from the root. After that, a line filter returns complete answers.
gronify flatten orders.jsonjson = {}json.service = {}json.service.name = "checkout"json.service.status = "degraded"json.orders = []json.orders[0] = {}json.orders[0].id = 1001json.orders[0].status = "shipped"json.orders[0].customer = {}json.orders[0].customer.email = "ada@example.com"json.orders[1] = {}json.orders[1].id = 1002json.orders[1].status = "Pending"json.orders[1].customer = {}json.orders[1].customer.email = "grace@example.com"json.orders[2] = {}json.orders[2].id = 1003json.orders[2].status = "pending"json.orders[2].customer = {}json.orders[2].customer.email = "alan@example.org"json.orders[2].error = {}json.orders[2].error.code = "CARD_DECLINED"Any line tool works on this output, so gronify flatten orders.json | grep pending is a valid pipeline. gronify search runs the flatten and the filter in one process. That saves a second tool on machines without grep, such as a stock Windows shell, and it lets Gronify stop reading the file as soon as it has the matches you asked for.
Search with plain text or a regex
gronify search <file> <term> prints every flattened line that contains the term. The term is compared against the whole line, so it can be part of a key, part of a value, or an array position such as orders[2].
gronify search orders.json pendingjson.orders[1].status = "Pending"json.orders[2].status = "pending"For patterns, add --regex (short form -r). The pattern is a JavaScript regular expression. Alternation, groups, anchors, and character classes behave as they do in grep -E. POSIX classes such as [[:digit:]] are not understood, so write [0-9]. Because each line starts with the path, a pattern can pin a match to one place in the document:
gronify search orders.json --regex "orders\[[0-9]+\]\.customer\.email"json.orders[0].customer.email = "ada@example.com"json.orders[1].customer.email = "grace@example.com"json.orders[2].customer.email = "alan@example.org"gronify search orders.json --regex 'status = "(pending|degraded)"'json.service.status = "degraded"json.orders[1].status = "Pending"json.orders[2].status = "pending"The first pattern returns the email of every order and skips any other line that happens to contain email. The second matches on the value side of the assignment, quotes included.
Case sensitivity
Search ignores case by default, which is the reverse of grep. That is why pending also found Pending above. Pass --case-sensitive (short form -c) for an exact-case match, with or without --regex.
gronify search orders.json --case-sensitive pendingjson.orders[2].status = "pending"In grep, -c counts matching lines. In Gronify, -c means case-sensitive, and counting is spelled --count.
Count, cap, and invert matches
Count the matches
--count prints the number of matching lines and nothing else.
gronify search orders.json --count status4Stop after the first matches
-m <n> (long form --max-count) prints the first n matches in document order and then stops reading the input, so a match near the top of a large file returns without scanning the rest. The notice on the last line goes to stderr, so it stays out of a pipe or a redirected file. A coding agent can sample a file the same way; see searching large JSON from a coding agent.
gronify search orders.json -m 2 statusjson.service.status = "degraded"json.orders[0].status = "shipped"Stopped after 2 matches (--max-count); there may be morePrint everything except a pattern
-v (long form --invert-match) keeps the lines that do not match.
gronify search orders.json -v ordersjson = {}json.service = {}json.service.name = "checkout"json.service.status = "degraded"Use the result in a shell script
By default gronify search exits 0 whether or not anything matched, so an empty result does not break an existing script. Add --exit-status to get grep's convention: 0 when at least one line matched, 1 when none did, and 2 for an error such as invalid JSON, a missing file, or a bad pattern.
gronify search --exit-status orders.json refunded; echo $?Warning: No matches found for 'refunded'Tip: Search is a case-insensitive substring match; use --regex for patterns1if gronify search --exit-status orders.json -m 1 CARD_DECLINED > /dev/null 2>&1; then echo "an order was declined"; fian order was declinedWith -m 1, Gronify stops reading at the first hit, which is all a yes-or-no test needs.
Search JSON from curl, gzip, or any pipe
Leave out the file name and Gronify reads standard input. The only argument left is the search term.
curl -s https://api.github.com/repos/1solomonwakhungu/gronify | gronify search -m 3 licensejson.license = {}json.license.key = "mit"json.license.name = "MIT License"Stopped after 3 matches (--max-count); there may be moregzip -dc orders.json.gz | gronify search -m 1 emailjson.orders[0].customer.email = "ada@example.com"Stopped after 1 match (--max-count); there may be moreInput is parsed as it arrives and is not loaded whole, so memory does not grow with the number of records. With -m, Gronify closes the pipe once it has enough matches. By default each run reads one JSON document. For JSON Lines (NDJSON) logs with one JSON value per line, pass -s, --stream, or --jsonl: gronify search --stream app.jsonl error flattens each line under json[0], json[1], and so on. For files in the gigabytes, see searching large JSON files.
Turn matches back into JSON
gronify unflatten reads flattened lines and writes JSON. Missing parent containers (including the root json = {} and any enclosing objects or arrays) are filled in automatically, so you can pipe gronify search straight into gronify unflatten:
gronify search orders.json service | gronify --pretty unflatten{ "service": { "name": "checkout", "status": "degraded" }}gronify search orders.json pending | gronify --pretty unflatten{ "orders": [ null, { "status": "Pending" }, { "status": "pending" } ]}Skipped array indexes are padded with null so each match keeps its original position. To drop one subtree and rebuild everything else instead, use an inverted search (-v):
gronify search orders.json -v customer | gronify unflatten{"service":{"name":"checkout","status":"degraded"},"orders":[{"id":1001,"status":"shipped"},{"id":1002,"status":"Pending"},{"id":1003,"status":"pending","error":{"code":"CARD_DECLINED"}}]}When jq or ripgrep is the better choice
Searching flattened paths suits a document whose shape you do not know yet, or a one-off look that is not worth a query. Other jobs have better tools.
jq is the right choice once you know the path. It selects whole records by a condition and returns a different field of the same record, which a line filter cannot do:
jq -r '.orders[] | select(.status == "pending") | .customer.email' orders.jsonalan@example.orgripgrep is the right choice for searching a directory of many files at once, or for a plain test of whether a string occurs anywhere. It does not parse JSON, so it cannot report a path. For a single NDJSON log where you want the path inside each record, pass --stream to gronify search; for raw line matches across many log files, ripgrep is faster.
| Job | Tool | Why |
|---|---|---|
| Find where a key or value lives in an unfamiliar document | gronify search | Each match prints with its full path |
| Search a JSON Lines (NDJSON) file by path | gronify search --stream | Numbers each line as json[0], json[1], ... with full paths |
| Read a field whose path you already know | jq | .orders[2].status is the whole query |
| Select records by a condition, or reshape the output | jq | It has select, map, and arithmetic |
| Search a directory of many files | ripgrep (rg) | It walks directories and matches line by line |
| Check whether a string occurs anywhere in a file | grep -q or ripgrep (rg) | No JSON parsing is needed |
Use this workflow with a coding agent
To teach Claude Code, Cursor, Codex, or another supported agent to grep nested JSON with --no-color, --count, and -m instead of reading large files whole, install the gronify-json-flatten-search skill (see JSON exploration for AI coding agents):
npx skills add 1solomonwakhungu/gronify --skill gronify-json-flatten-search