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

Document how to stream JSON arrays #96

Merged
merged 3 commits into from
Aug 12, 2023
Merged

Document how to stream JSON arrays #96

merged 3 commits into from
Aug 12, 2023

Conversation

ehmicky
Copy link
Collaborator

@ehmicky ehmicky commented Aug 11, 2023

This PR documents how to parse JSON array streams incrementally, as opposed to blocking the event loop once at the end using JSON.parse().

This does not cover actual JSON streams formats (where each line is a single object, and there are no [ ] delimiters) although the utility mentioned supports those too.

readme.md Outdated

const stream = fs.createReadStream('big-array-of-objects.json', {encoding: 'utf8'});
console.log(await getStreamAsArray(
stream.pipe(streamJson.parser()).pipe(streamJsonArray.streamArray()),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pipeline is generally recommended instead of pipe methods.

Copy link
Collaborator Author

@ehmicky ehmicky Aug 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right! Good catch.

What about compose() instead? While pipeline() consumes the stream and returns a promise resolving when done (which would conflict with getStreamAsArray() consuming it too), compose() does not consume it and returns the stream.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, compose is probably the right choice. I've been too used to (apparently) incorrectly using pipeline.

readme.md Outdated Show resolved Hide resolved
ehmicky and others added 2 commits August 12, 2023 23:58
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
@ehmicky
Copy link
Collaborator Author

ehmicky commented Aug 12, 2023

Fixed 👍

@sindresorhus sindresorhus merged commit 68d8859 into sindresorhus:main Aug 12, 2023
3 checks passed
@ehmicky ehmicky deleted the json-tip branch August 12, 2023 23:25
@ehmicky ehmicky mentioned this pull request Aug 13, 2023
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

Successfully merging this pull request may close these issues.

2 participants