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.
| Tool | Kind | Use it for |
|---|---|---|
| jaq | jq implementation in Rust | Running jq programs. Its README calls it "a clone of the JSON data processing tool jq" and adds YAML, CBOR, TOML, and XML support. |
| gojq | jq implementation in Go | Running jq programs with arbitrary-precision integer arithmetic and YAML input. It does not keep object key order. |
| fx | Interactive viewer and processor | Browsing a document in the terminal, or processing it with JavaScript expressions in place of jq syntax. |
| jless | Interactive viewer | Reading a document by hand: expand and collapse objects and arrays, move around, and search with a regex. |
| jnv | Interactive jq filter editor | Typing a jq filter next to a view of the document. It applies the filter with jaq, so jq does not have to be installed. |
| gron | Flattener in Go | The original path = value output for grep, with gron -u to turn filtered lines back into JSON. |
| fastgron | Flattener 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. |
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",148Gronify 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 moreThose 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:
- Drop the leading
json. What is left,.data.projects[0].envs[0].deploy.status, is a valid jq path to that one value. - Replace an index such as
[0]with[]to visit every element of the array:.data.projects[].envs[]. - Put the condition in
select, then name the field you want back. Its path,.deploy.commit.author.email, comes from theemailsearch 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.comThe 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.