Parsing items using JSON Stream

65 views Asked by At

I am quite new to the concept of streams in Node js. I am using the JSONStream package in node to parse an api response. The response is an array of javascript objects and is being streamed from the API to this node js service. The service which receives the streamed response writes the result into a file.

The response looks like this: [{key1:val1,key2:val2},{key1:val1,key2:val2}] The api response is stringified using JSON.stringify() before streaming

The write command in node js service looks like this: res.pipe(JSONStream.parse('*')).pipe(transformStream).pipe(ws) transformStream is a duplex stream to manipulate the data and ws is the writable.

However, am facing error in the JSONStream.parse('*'). The error message is as follows "Error: Unexpected RIGHT_BRACKET("]") in state VALUE"

It is because of the right bracket in the response. Any leads would be helpful. I am quite confused with understanding the JSONStream syntax as well. JSONStream.parse(path). Can this path argument here be left blank or what is the significance of using '*' in the path?

I tried using the '{}' in place of the '[]' and tried returning an object of objects from the api service. Received the error message - "Error: Unexpected RIGHT_BRACKET("]") in state VALUE"

0

There are 0 answers