Skip to main content

A jq alternative for exploring JSON you do not know yet

Gronify does not replace jq: jq is the right tool for reshaping, filtering, and computing over JSON whose structure you already know. The alternative is for the step before that, when a payload is new to you and you need to learn where things are. gronify search deploys.json failed prints json.data.projects[0].envs[0].deploy.status = "failed", the full path next to the value, and that path is what the jq filter is written from.

Updated

Other jq alternatives and what each is for

Tools that get listed as jq alternatives do one of three things. Some run jq's own language in a different implementation, some are interactive viewers, and some flatten JSON for line tools the way gron does.

ToolKindUse it for
jaqjq implementation in RustRunning jq programs. Its README calls it "a clone of the JSON data processing tool jq" and adds YAML, CBOR, TOML, and XML support.
gojqjq implementation in GoRunning jq programs with arbitrary-precision integer arithmetic and YAML input. It does not keep object key order.
fxInteractive viewer and processorBrowsing a document in the terminal, or processing it with JavaScript expressions in place of jq syntax.
jlessInteractive viewerReading a document by hand: expand and collapse objects and arrays, move around, and search with a regex.
jnvInteractive jq filter editorTyping a jq filter next to a view of the document. It applies the filter with jaq, so jq does not have to be installed.
gronFlattener in GoThe original path = value output for grep, with gron -u to turn filtered lines back into JSON.
fastgronFlattener in C++gron output on big files. Its README says it is 50x faster than gron there, and it adds a fixed-string filter (-F) and path queries.
jq alternatives by kind and by the job each one fits

Only jaq and gojq replace jq in the literal sense, and neither removes the exploration step, because their filters need a path too. jaq 3.1.2 printed the same output as jq 1.8.2 for every filter on this page that succeeds. How the three flatteners differ is covered in the gron alternatives comparison.

When jq is the right tool

jq is a query language, and its output can have a different shape from its input. Gronify has three commands, flatten, search, and unflatten, and no query language, so it shows what is in a document and cannot compute anything new from it. The examples on this page use one API response saved as deploys.json:

cat deploys.json{"meta":{"request_id":"req_8f3a","took_ms":42},"data":{"workspace":{"id":"ws_acme","plan":"team"},"projects":[{"name":"api","envs":[{"name":"prod","deploy":{"status":"failed","duration_s":312,"commit":{"sha":"9c1e4b7","author":{"email":"ada@example.com"}}}},{"name":"staging","deploy":{"status":"ok","duration_s":95,"commit":{"sha":"9c1e4b7","author":{"email":"ada@example.com"}}}}]},{"name":"web","envs":[{"name":"prod","deploy":{"status":"ok","duration_s":148,"commit":{"sha":"41d0aa2","author":{"email":"grace@example.com"}}}}]}]}}

Three jobs on that file need jq: building new objects from parts of old ones, adding up a field, and writing CSV with a value from a parent record on every row.

jq -c '.data.projects[] | {project: .name, envs: [.envs[].name]}' deploys.json{"project":"api","envs":["prod","staging"]}{"project":"web","envs":["prod"]}jq '[.data.projects[].envs[].deploy.duration_s] | add' deploys.json555jq -r '.data.projects[] | .name as $p | .envs[] | [$p, .name, .deploy.status, .deploy.duration_s] | @csv' deploys.json"api","prod","failed",312"api","staging","ok",95"web","prod","ok",148

Gronify cannot do any of the three. If you already know the paths, use jq and skip Gronify. One caveat on arithmetic: jq 1.7 and later print a number as written until a calculation touches it, and then it becomes a double, which the guide to JSON number precision covers.

Exploring unknown JSON structure with jq

Every one of those filters starts from a path, and with a new payload you do not have one. The usual way to get it is to ask for the keys one level at a time:

jq -c keys deploys.json["data","meta"]jq -c '.data | keys' deploys.json["projects","workspace"]jq -c '.data.projects | keys' deploys.json[0,1]jq -c '.data.projects[0] | keys' deploys.json["envs","name"]jq -c '.data.projects[0].envs[0] | keys' deploys.json["deploy","name"]jq -c '.data.projects[0].envs[0].deploy | keys' deploys.json["commit","duration_s","status"]

Six commands reach status, and they describe only the first environment of the first project. keys sorts the names, so data prints before meta although the file has them the other way round. A guess that skips an array index stops with an error:

jq '.data.projects.envs' deploys.jsonjq: error (at deploys.json:1): Cannot index array with string ("envs")

jq also has builtins that walk the whole document. to_entries lists keys in document order, here with the type of each value. paths prints every path, and paths(f) prints the paths whose value passes a test:

jq -c 'to_entries | map({key, type: (.value | type)})' deploys.json[{"key":"meta","type":"object"},{"key":"data","type":"object"}]jq -c paths deploys.json | head -4["meta"]["meta","request_id"]["meta","took_ms"]["data"]jq -c 'paths(. == "failed")' deploys.json["data","projects",0,"envs",0,"deploy","status"]

The last command answers the question in one step, so jq can do this job. It takes a new filter for each question, == compares the whole value (a substring needs paths(strings | test("fail"))), and the result is an array of path segments without the value. You then rewrite that array as .data.projects[0].envs[0].deploy.status by hand.

The same exploration with gronify flatten and search

A gron-style tool prints the document as one path = value line per value, so the structure and the data arrive together. gron introduced the format, and its README answers the question "Why shouldn't I just use jq?" by calling jq "a lot more powerful than gron, but with that power comes complexity". It goes on: "gron's primary purpose is to make it easy to find the path to a value in a deeply nested JSON blob when you don't already know the structure; much of jq's power is unlocked only once you know that structure." (gron README)

Gronify is a gron-family CLI for Node.js 20 or newer, installed with npm install -g gronify (install options). Start with the top of the file. --limit stops reading the input at the cap, so the rest of a large file is never read:

gronify flatten deploys.json --limit 12json = {}json.meta = {}json.meta.request_id = "req_8f3a"json.meta.took_ms = 42json.data = {}json.data.workspace = {}json.data.workspace.id = "ws_acme"json.data.workspace.plan = "team"json.data.projects = []json.data.projects[0] = {}json.data.projects[0].name = "api"json.data.projects[0].envs = []Stopped after 12 lines (--limit); there may be more

Those lines show the top-level keys in document order, that projects is an array of objects, and that each project has a name and an array called envs. To locate one thing, search for a word you expect to find in it:

gronify search deploys.json failedjson.data.projects[0].envs[0].deploy.status = "failed"gronify search deploys.json emailjson.data.projects[0].envs[0].deploy.commit.author.email = "ada@example.com"json.data.projects[0].envs[1].deploy.commit.author.email = "ada@example.com"json.data.projects[1].envs[0].deploy.commit.author.email = "grace@example.com"

Each match is a full path with its value, array indexes included, and neither search needed a filter. The term is matched as a case-insensitive substring of the whole line, so it can sit in a key or in a value. Regex, counting, and the other search flags are in the guide to grepping JSON.

From one path to a jq filter over every record

The path Gronify prints is close to a jq filter already. Three edits turn json.data.projects[0].envs[0].deploy.status into a query over every deploy:

  1. Drop the leading json. What is left, .data.projects[0].envs[0].deploy.status, is a valid jq path to that one value.
  2. Replace an index such as [0] with [] to visit every element of the array: .data.projects[].envs[].
  3. Put the condition in select, then name the field you want back. Its path, .deploy.commit.author.email, comes from the email search above.
jq '.data.projects[0].envs[0].deploy.status' deploys.json"failed"jq -r '.data.projects[].envs[] | select(.deploy.status == "failed") | .deploy.commit.author.email' deploys.jsonada@example.com

The second command selects a record by one field and returns another, which a line filter cannot do. Bracketed keys carry over unchanged: Gronify prints json.headers["content-type"], and .headers["content-type"] is valid jq. A key that contains $ is the exception: Gronify prints it bare, as in json.headers.$ref, and jq needs .headers["$ref"]. More cases, such as searching by key name only, are in the guide to finding a JSON path.

Frequently asked questions

How do I list all keys recursively in jq?

jq -c '[.. | objects | keys[]] | unique' deploys.json prints every distinct key name in the document as one array, and jq -c paths deploys.json prints every path as an array of segments. To see each key with its position and its value, run gronify flatten deploys.json, which prints one path = value line per value.

Can Gronify read JSON Lines or NDJSON the way jq does?

Yes, with -s, --stream, or --jsonl. gronify flatten --stream lines.ndjson and gronify search --stream lines.ndjson term read one JSON value per line and number them json[0], json[1], and so on under json = [], matching gron -s and fastgron -s. Without the flag Gronify expects one JSON document per run, while jq reads a sequence of documents by default (jq -c .a lines.ndjson).

Can Gronify change a value in a JSON file?

Only by editing lines. gronify flatten deploys.json | sed 's/"failed"/"ok"/' | gronify unflatten writes the document back with that value replaced, because unflatten receives every line. Adding a field to each record, deleting records by a condition, and other structural edits are jq's job.

Does jq load the whole JSON file into memory?

Without --stream, jq parses each JSON text completely before the filter runs. With --stream, the program receives [path, leaf] events in place of the document and has to rebuild values itself. gronify flatten and gronify search read the input in chunks and write each line as it is produced, with no flag. Measured time and memory for both tools are in the benchmarks.

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