Findlay
Findlay

Reputation: 134

Parse whole object with JSONStream.parse

I have an incoming JSON data object structure like this:

{
    foo: 3,
    bar: [
             {
                 key:value
             },
             {
                 key:value
             }
         ]
}

I want to treat foo one way, and bar another. But when I use JSONStream.parse("*") on the object, the first chunk it receives is "3".

Since the data objet is quite small, I want the streamer to return the whole object and I'll then manipulate it. I'd still like to use streams to be consistent with the rest of the project though. How do I force JSONStream to give me the whole object?

Upvotes: 1

Views: 1910

Answers (1)

Artur Pschybysz
Artur Pschybysz

Reputation: 303

Its a little late but I have managed to do it like this:

let obj = {foo:{}, bar:[]}

const fooStream = JSONStream.parse(["foo", true]);
fooStream.on("data", (data)=>{obj.foo = data;})

const barStream = JSONStream.parse(["bar", true]);
barStream.on("data", (data)=>{obj.bar.push(data);})

_some_stream.pipe(fooStream);
_some_stream.pipe(barStream);

Upvotes: 1

Related Questions