'How to split incoming json stream into individual json messages in NodeJS
I am looking for a solution which could be piped to my existing streams so that this input:
{ "foo":"bar" }{ "foo": { "foo": "bar" }}
Would display this:
{ "foo":"bar" }
{ "foo": { "foo": "bar" }}
Using this code:
incomingStream.pipe(jsonSplitter()).on('data', (singleJson) => {
console.log(singleJson)
});
Any packages?
Solution 1:[1]
Ok so you can use stream-json npm package. In order to achieve what I asked in the question you would need this code:
import { parser } from 'stream-json';
import { streamObject } from 'stream-json/streamers/StreamObject';
...
stream
.pipe(parser({ jsonStreaming: true }))
.pipe(streamObject())
.on('data', (data: any) => {
console.log(data);
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ablaszkiewicz1 |
