Reputation: 2022
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"}.........]
Upvotes: 0
Views: 7527
Reputation: 281
var buf = Buffer.from(JSON.stringify(obj));
var temp = JSON.parse(buf.toString());
Upvotes: 3
Reputation: 143
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()?
Upvotes: 0