I have the following string that I would like to parse to JSON :
{
"STATUS": [
{
"STATUS": "S",
"When": 1394044643,
"Code": 17,
"Msg": "GPU0",
"Description": "cgminer 3.7.3"
}
],
"GPU": [
{
"GPU": 0,
"Enabled": "Y",
"Status": "Alive",
"Temperature": 70,
"Fan Speed": 3089,
"Fan Percent": 70,
"GPU Clock": 1180,
"Memory Clock": 1500,
"GPU Voltage": 1.206,
"GPU Activity": 99,
"Powertune": 20,
"MHS av": 0.4999,
"MHS 5s": 0.5009,
"Accepted": 4335,
"Rejected": 7,
"Hardware Errors": 0,
"Utility": 27.8007,
"Intensity": "0",
"Last Share Pool": 0,
"Last Share Time": 1394044643,
"Total MH": 4676.7258,
"Diff1 Work": 69436,
"Difficulty Accepted": 69360,
"Difficulty Rejected": 112,
"Last Share Difficulty": 16,
"Last Valid Work": 1394044643,
"Device Hardware%": 0,
"Device Rejected%": 0.1613,
"Device Elapsed": 9356
}
],
"id": 1
}
When I use e.g. http://jsonlint.com/ it says that the JSON is correct but when I use in node.js:
console.log(JSON.parse(data.toString()));
I get the following :
undefined:1
e Hardware%":0.0000,"Device Rejected%":0.1570,"Device Elapsed":9554}],"id":1}
^
SyntaxError: Unexpected token
Any clue what am I doing wrong here ?
EDIT
The data is coming as ByteStream :
.on('data',function(data){
console.log(data.toString());
console.log();
console.log(data);
console.log();
console.log("data "+ data.GPU);
//...
//...
{"STATUS":[{"STATUS":"S","When":1394045650,"Code":17,"Msg":"GPU0","Description":"cgminer 3.7.3"}],"GPU":[{"GPU":0,"Enabled":"Y","Status":"Alive","Temperature":70.00,"Fan Speed":3090,"Fan Percent":70,"GPU Clock":1180,"Memory Clock":1500,"GPU Voltage":1.206,"GPU Activity":99,"Powertune":20,"MHS av":0.4999,"MHS 5s":0.5007,"Accepted":4841,"Rejected":8,"Hardware Errors":0,"Utility":28.0261,"Intensity":"0","Last Share Pool":0,"Last Share Time":1394045638,"Total MH":5181.3734,"Diff1 Work":77548,"Difficulty Accepted":77456.00000000,"Difficulty Rejected":128.00000000,"Last Share Difficulty":16.00000000,"Last Valid Work":1394045638,"Device Hardware%":0.0000,"Device Rejected%":0.1651,"Device Elapsed":10364}],"id":1}
<Buffer 7b 22 53 54 41 54 55 53 22 3a 5b 7b 22 53 54 41 54 55 53 22 3a 22 53 22 2c 22 57 68 65 6e 22 3a 31 33 39 34 30 34 35 32 34 38 2c 22 43 6f 64 65 22 3a 31 ...>
data undefined
EDIT
When I do this :
console.log(data.toString());
console.log(JSON.stringify(data.toString()));
I get the following result :
"{"STATUS":[{"STATUS":"S","When":1394046864,"Code":17,"Msg":"GPU0","Description":"cgminer 3.7.3"}],"GPU":[{"GPU":0,"Enabled":"Y","Status":"Alive","Temperature":70.00,"Fan Speed":3087,"Fan Percent":70,"GPU Clock":1180,"Memory Clock":1500,"GPU Voltage":1.206,"GPU Activity":99,"Powertune":20,"MHS av":0.5000,"MHS 5s":0.5016,"Accepted":5396,"Rejected":8,"Hardware Errors":0,"Utility":27.9597,"Intensity":"0","Last Share Pool":0,"Last Share Time":1394046864,"Total MH":5789.2352,"Diff1 Work":86428,"Difficulty Accepted":86336.00000000,"Difficulty Rejected":128.00000000,"Last Share Difficulty":16.00000000,"Last Valid Work
":1394046864,"Device Hardware%":0.0000,"Device Rejected%":0.1481,"Device Elapsed":11580}],"id":1}"
"{\"STATUS\":[{\"STATUS\":\"S\",\"When\":1394046864,\"Code\":17,\"Msg\":\"GPU0\",\"Description\":\"cgminer 3.7.3\"}],\"GPU\":[{\"GPU\":0,\"Enabled\":\"Y\",\"Status\":\"Alive\",\"Temperature\":70.00,\"Fan Sp
eed\":3087,\"Fan Percent\":70,\"GPU Clock\":1180,\"Memory Clock\":1500,\"GPU Voltage\":1.206,\"GPU Activity\":99,\"Powertune\":20,\"MHS av\":0.5000,\"MHS 5s\":0.5016,\"Accepted\":5396,\"Rejected\":8,\"Hardw
are Errors\":0,\"Utility\":27.9597,\"Intensity\":\"0\",\"Last Share Pool\":0,\"Last Share Time\":1394046864,\"Total MH\":5789.2352,\"Diff1 Work\":86428,\"Difficulty Accepted\":86336.00000000,\"Difficulty Re
jected\":128.00000000,\"Last Share Difficulty\":16.00000000,\"Last Valid Work\":1394046864,\"Device Hardware%\":0.0000,\"Device Rejected%\":0.1481,\"Device Elapsed\":11580}],\"id\":1}\u0000"
Notice the last unicode character of the second message \u0000
what can I do with it?
The problem was with the terminating null character. After removing it I can now parse the string no problemo ( post more efficient way if you have it)
var re = /\0/g;
str = data.toString().replace(re, "");
var o = JSON.parse(str);
console.log(o);
or
var str = data.toString().slice(0, - 1);
or work directly on bytes in Buffer
var buf = data.slice(0,data.length-1);
console.log(JSON.parse(buf.toString()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With