# Convert a Buffer into a JavascriptObject

**URL:** <https://discourse.nodered.org/t/convert-a-buffer-into-a-javascriptobject/4879>\
**Category:** General\
**Created:** [14 November 2018 21:09 UTC](https://discourse.nodered.org/t/convert-a-buffer-into-a-javascriptobject/4879 "2018-11-14T21:09:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubaleeiro](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@dubaleeiro](https://discourse.nodered.org/u/dubaleeiro)\
**Post date:** [14 November 2018 21:09 UTC](https://discourse.nodered.org/t/convert-a-buffer-into-a-javascriptobject/4879/1 "2018-11-14T21:09:43Z")

</div>

Hello,  
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};

Buffer:  
{"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 !!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 November 2018 21:33 UTC](https://discourse.nodered.org/t/convert-a-buffer-into-a-javascriptobject/4879/2 "2018-11-14T21:33:30Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![dubaleeiro](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@dubaleeiro](https://discourse.nodered.org/u/dubaleeiro)\
**Post date:** [14 November 2018 22:17 UTC](https://discourse.nodered.org/t/convert-a-buffer-into-a-javascriptobject/4879/3 "2018-11-14T22:17:02Z")

</div>

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