简体   繁体   中英

Convert a node.js buffer to string and then to JSON

I have a node.js readstream which emits a buffer and using toString() function i convert the buffer to string and after that when i try to convert the string to JSON via JSON.parse() function it throws parse error.

Is there a best way to convert buffer to string and then that string to JSON?

JSON String looks like below,

[{"data1": 1487328824948, "encrypt": false, "version": "1.0.0", "data2": "value2", "data3": "value3", "data4": "value4", "data5": "value5"},{"data1": 148732882448, "encrypt": false, "version": "1.0.0", "data2": "value2", "data3": "value3", "data4": "value4", "data5": "value5"}.........]

var buf = Buffer.from(JSON.stringify(obj));
var temp = JSON.parse(buf.toString());

This seems like the right way, but it seems likely that your readstream isn't finishing reading the input before you call JSON.parse(). Therefore the JSON.parse() call only parses part of your JSON string and then you get the error.

Try making sure the read() finishes - use readSync() ?

I was able to parse the incoming stream using JSONStream package. https://github.com/dominictarr/JSONStream , really helped me in this use case, a nice and handy tool.

Related StackOverflow post - Parse large JSON file in Nodejs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM