gojq icon indicating copy to clipboard operation
gojq copied to clipboard

Question about handling NDJSON with the library

Open noahgorstein opened this issue 1 year ago • 1 comments

Hey - thanks for the fantastic library and apologies in advance if this is the wrong place for this question. I was wondering if there is a preferred way of handling NDJSON (also known as JSON lines iiuc) with the library. I noticed the CLI seems to handle this fine and the same as jq. I haven't spent the time to read/trace the code yet to see what's being done there.

❯ cat ex.jsonl
{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]}
{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]}
{"name": "May", "wins": []}
{"name": "Deloise", "wins": [["three of a kind", "5♣"]]}

❯ cat ex.jsonl | gojq '.name'
"Gilbert"
"Alexa"
"May"
"Deloise"

I was thinking of naively just putting sticking all the JSON objects a slice ([]interface{}) and passing that to query.Run/query.RunWithContext but then I'd need to effectively apply the .[] to get my desired result. Is that what's being done in the CLI or is there a bit more to it?

noahgorstein avatar Apr 16 '24 01:04 noahgorstein

I recommend to run the query against each JSON object, not collecting all the objects to a slice and run once. This way works even if the input file has a lot of JSONs, and that's what the CLI does.

itchyny avatar Apr 16 '24 03:04 itchyny