Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about handling NDJSON with the library #252

Closed
noahgorstein opened this issue Apr 16, 2024 · 1 comment
Closed

Question about handling NDJSON with the library #252

noahgorstein opened this issue Apr 16, 2024 · 1 comment

Comments

@noahgorstein
Copy link

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
Copy link
Owner

itchyny commented Apr 16, 2024

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 itchyny closed this as completed May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants