sanjanakaranam
sanjanakaranam

Reputation: 1

How to convert one line JSON to JSONLINES format using jq?

I'm trying to convert a big one line JSON file to a multiline JSONLINES file. I've tried a bunch of commands and none of them are doing what I expect them to. This is the format of my data (input file: response.json) -

{"_rid":"12345","Documents":[{"id": "id-1", "name": "name-1", "userId": "userid-1", "_rid": "rid-1"},{"id": "id-2", "name": "name-2", "userId": "userid-2", "_rid": "rid-2"},{"id": "id-3", "name": "name-3", "userId": "userid-3", "_rid": "rid-3"},....],"_count":566}

I want to get rid of the unnecessary characters and make each record its own line. I want to get rid of the very first _rid but not the ones inside the individual records. My desired output looks like this -

{"id": "id-1", "name": "name-1", "userId": "userid-1", "_rid": "rid-1"}
{"id": "id-2", "name": "name-2", "userId": "userid-2", "_rid": "rid-2"}
{"id": "id-3", "name": "name-3", "userId": "userid-3", "_rid": "rid-3"}
....

These are some queries I've tried (I know some of these aren't going to work but I was desperate and tried so many things):

jq --slurp '.' response.json > output.jsonl
jq -c '.[]' | jq ."Documents" response.json > output.jsonl
jq '.[] | select("Documents")' response.json > output.jsonl
jq '.[]' | jq 'select("Documents")' response.json > output.jsonl
jq --slurp --compact-output 'map([ . ])' response.json > output.jsonl

How do I do this using jq? Thanks!

Upvotes: 0

Views: 152

Answers (1)

oguz ismail
oguz ismail

Reputation: 50805

Almost there

jq -c '.Documents[]' response.json

Online demo

Upvotes: 1

Related Questions