My request sounds trivial but I could not find a way to do it. I have as input an array of JSON objects:
[ { "foo": 1, "bar": 2 }, { "foo": 3, "bar": 4 }, (...) ]
and I want as output the JSONL version of the same, aka one object per line, not an array:
{ "foo": 1, "bar": 2 } { "foo": 3, "bar": 4 } (...)
This is not the same as using --compact-output
, as that would preserve the array and give me:
[ { "foo": 1, "bar": 2 }, { "foo": 3, "bar": 4 }, (...) ]
Thank you in advance.
The slurp option ( -s ) changes the input to the jq program. It reads all the input values and build an array for the query input. Using with the raw input option ( -R ) means reading the entire input as a string. The inputs function is a special stream that emits the remaining JSON values given to the jq program.
jq is a lightweight and flexible command-line JSON processor. If you are a command line addict, you will like the official description. jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
jq is a command-line tool for parsing JSON. Most of the popular API and data services use the JSON data format, so we'll learn how it's used to serialize interesting information, and how to use the jq to parse it at the command-line.
The answer to the original question is to use the filter .[]
together with the -c
command-line option:
$ jq -c '.[]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With