Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jq to output JSONL (one independent JSON object per line)

Tags:

json

jq

jsonlines

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.

like image 591
giacecco Avatar asked Feb 11 '17 17:02

giacecco


People also ask

What is jq slurp?

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.

What is JSON jq?

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.

What is jq parser?

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.


1 Answers

The answer to the original question is to use the filter .[] together with the -c command-line option:

$ jq -c '.[]' 
like image 159
peak Avatar answered Sep 21 '22 15:09

peak