|
|
|
|
|
by naniwaduni
406 days ago
|
|
> The leading "{}" is moderately necessary because `jq` technically _processes_ json, not generates it. The --null-input/-n option is the "out-of-the-box" way to achieve this, and avoids a pipe (usually not a big deal, but leaves stdin free and sometimes saves a fork). This lets you rewrite your first "pattern": jq -cnr --argjson SOME_JSON "$SOME_JSON" '$SOME_JSON[]' | while read ...
We also have a "useless use of cat": --slurpfile does that job better: jq -n --slurpfile FOO test.json '{bar: $FOO[]}'
(assuming you are assured that test.json contains one json value; --argjson will immediately fail if this is not the case, but with --slurpjson you may need to check that $FOO is a 1-item array.)And of course, for exactly the single-file single-object case, you can just write: jq '{bar: .}' test.json
|
|
Pipelines allow consistent syntax, but thanks for pointing out all the different variations of file support in jq.