itchyny / gojq

Pure Go implementation of jq
MIT License
3.26k stars 118 forks source link

Question about handling NDJSON with the library #252

Closed noahgorstein closed 4 months ago

noahgorstein commented 5 months ago

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?

itchyny commented 5 months ago

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.