Convert a Buffer into a JavascriptObject


I am trying to convert the following buffer out of an sensor into a Javascript Object withou any success...

Can anyone plz help me? I am trying to convert the raw data throught the following code:

var XDK_str = msg.payload.toString();
var XDK_parsed = JSON.parse(XDK_str);
return {XDK_str};

{"xdk2mam":[{"sensorType": "Environmental","data":[{"name":"Pressure","value":"98186"},{"name":"Temperature","value":"28030"},{"name":"Humidity","value":"37"}]},{"sensorType":"Accelerometer","data":[{"name":"x","value":"13"},{"name":"y","value":"13"},{"name":"z","value":"999"}]},{"sensorType":"Gyroscope","data":[{"name":"x","value":"-2197"},{"name":"y","value":"1342"},{"name":"z","value":"1220"}]},{"sensorType":"Inertial","data":[{"name":"x","value":"122"},{"name":"y","value":"488"},{"name":"z","value":"488"}]},{"sensorType":"Light","data":[{"name":"milliLux","value":"18900"}]},{"sensorType":"Magnetometer","data":[{"name":"x","value":"9"},{"name":"y","value":"-18"},{"name":"z","value":"-44"}]} ],"device": "XDK_MQTT","timestamp": "7717"}

Thanks !!



When you say buffer, do you mean a javascript Buffer? If you are not sure then feed it into a debug node and show us what you see.

In addition a problem with your function is that you are not returning a correctly constructed object. You might want to use

msg.payload = XDK_parsed;
return msg

Did you try just feeding the buffer into a JSON node? It is designed for strings but it might work with a buffer, I don't know.

Finally when posting things like your buffer or a flow put a line containing three backtick characters before it and another such line after it. That will prevent the forum from reformatting it, putting in fancy quotes and so on.



Hi Colin,
It worked now after feeding the output our the function to a JSON node.
Thanks a lot for the hints !