问题
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.
回答1:
The answer to the original question is to use the filter .[]
together with the -c
command-line option:
$ jq -c '.[]'
回答2:
Found: it's
map(tostring) | reduce .[] as $item (""; . + $item + "\n")
You also need to use --raw-output
.
来源:https://stackoverflow.com/questions/42178636/how-to-use-jq-to-output-jsonl-one-independent-json-object-per-line