Skip to content
Advertisement

How do I use the pipeline from stream-json to write to file, in nodeJs?

I’m trying to use stream-json to read a zip, unzip it, and then write it to file. I don’t think I understand how to use the library.

Based on the link above, they have this example:

JavaScript

However I don’t want to count anything, I just want to write to file. Here is what I have that works:

JavaScript

However I can’t easily add any processing to this, so I wanted to replace it with stream-json. This is my partial attempt, as I don’t know how to finish:

JavaScript

Later on I intend to add extra processing of the json file(s), but I want to learn the basics before I start throwing in extra functionality.

I can’t produce a minimal example unfortunately, as I don’t know what goes into the pipeline.on function. I’m trying to understand what I should do, not what I’ve done wrong.

I also looked at the related stream-chain, which has an example that ends like so:

JavaScript

But at no point does the documentation explain where dataSource comes from, and I think my chain creates it’s own by reading the zip from file?

How am I supposed to use these streaming libraries to write to file?

Advertisement

Answer

I don’t want to count anything, I just want to write to file

In that case, you’ll need to convert the token/JSON data stream back into a text stream that you can write to a file. You can use the library’s Stringer for that. Its documentation also contains an example that seems to be more in line with what you want to do:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement