# Convert Buffer to proper object

**URL:** <https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020>\
**Category:** General\
**Tags:** mqtt\
**Created:** [11 December 2022 08:52 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020 "2022-12-11T08:52:31Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shri360](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shri360/32/37341_2.png) [@shri360](https://discourse.nodered.org/u/shri360)\
**Post date:** [11 December 2022 08:52 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/1 "2022-12-11T08:52:31Z")

</div>

Hello,

i'm getting buffer in msg.payload like this. (from mqtt)

`[1,3,80,0,0,3,232,0,0,5,93,0,0,0,0,5,93,42,224,26,207,37,138,107,57,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0]`

i want it like this

```auto
{
    "Device_ID": "CC50E3EBD81D",
    "Time": "2022-11-28T10:58:45.425Z",
    "PrdWt": 1200,
    "nOff": 0,
    "nPChg": 2853,
    "nCChg": 0,
    "nOvrL": 0,
    "nEOff": 2853,
    "ovrTl": 22824,
    "winTl": 14266,
    "blwTl": 19971,
    "tPrdOK": 57061,
    "DPvalue": 2
}

```

how to convert it?

i'm newbie in node-red. please help me to get proper data. 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:** [11 December 2022 09:08 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/2 "2022-12-11T09:08:53Z")

</div>

Do you mean that the numbers in the buffer are just the values that you wish to populate the object with, or do you think that whole object, including field names is contained in that buffer? It isn't long enough for that.

If the former, then how does the data in the buffer map onto the object?

---

<div class="post-metadata">

**Author:** ![shri360](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shri360/32/37341_2.png) [@shri360](https://discourse.nodered.org/u/shri360)\
**Post date:** [11 December 2022 09:11 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/3 "2022-12-11T09:11:54Z")

</div>

buffer contains just values and need to populate with object.

thanks!!

---

<div class="post-metadata">

**Author:** ![shri360](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shri360/32/37341_2.png) [@shri360](https://discourse.nodered.org/u/shri360)\
**Post date:** [11 December 2022 09:15 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/4 "2022-12-11T09:15:19Z")

</div>

12 number of data sending from IOT module to MQTT broker. like this

 ![image009](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b5d2af47954b342e77ab3d0ca5884044ca3ab8a1.png)

---

<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:** [11 December 2022 09:21 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/5 "2022-12-11T09:21:51Z")

</div>

That doesn't seem to make sense. The buffer starts with

> [@shri360](#):
>
> [1,3,80,0,0, ...

I don't see how that can contain the string "CC50E3EBD81D"

Similarly tProOK should be 57061, but the end of the buffer is all zeros.

---

<div class="post-metadata">

**Author:** ![shri360](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shri360/32/37341_2.png) [@shri360](https://discourse.nodered.org/u/shri360)\
**Post date:** [11 December 2022 09:22 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/6 "2022-12-11T09:22:19Z")

</div>

if i use `msg.payload = msg.payload.toString(); return msg;`

getting like this.

P

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 December 2022 09:27 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/7 "2022-12-11T09:27:53Z")

</div>

How is your mqtt in node configured?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 December 2022 09:32 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/9 "2022-12-11T09:32:56Z")

</div>

The document does not tell me how you configured your mqtt in node. I was looking to see what the output is configured to.  
P.S. The data looks compressed when outputed as a string from the mqtt in node.

---

<div class="post-metadata">

**Author:** ![shri360](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shri360/32/37341_2.png) [@shri360](https://discourse.nodered.org/u/shri360)\
**Post date:** [11 December 2022 10:43 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/10 "2022-12-11T10:43:03Z")

</div>

okay, how can i do this calculation for bytes?

PrdWt = byte 6 \*256 + byte 7  
nOff = byte 8 \* 256 + byte

because packet constructed like that only.

it's possible in buffer-parser node?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 December 2022 10:45 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/11 "2022-12-11T10:45:11Z")

</div>

According to the manual you linked, data starts at byte 6, and every value is `(256*byte-n) + (byte-n1)`

> The mqtt format will be a packet of 40 hex bytes. Each key value in the json(e.g PrdWt), is constructed as 256\*byte0+byte1.  
> The offset of the 1st value in the json (“PrdWt”) is the 6th byte offset of the byte stream. So,  
> PrdWt = byte 6 \*256 + byte 7  
> nOff = byte 8 \* 256 + byte 9 and so on

Essentially, the data is a byte reversed array of 16bit values with data starting at byte 6.

Following that specification, using the buffer-parser node like this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/7/c7b283eeddcce42c8b0fac5260ddb893896c2acf.png)

...with your buffer...

> [@shri360](#):
>
> [1,3,80,0,0,3,232,0,0,5,93,0,0,0,0,5,93,42,224,26,207,37,138,107,57,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0]

produces this...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9ff499cc3438353b3d3ce567d8502d53dd8597ec.png)

I have no idea if that is correct or not (I suspect the buffer data you provided does NOT match the result in your first post)

Demo flow...

```auto
[{"id":"2df7243189d4a33a","type":"inject","z":"1b5ed5b363954ae7","name":"inject your buffer","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"CC50E3EBD81D/event","payload":"[1,3,80,0,0,3,232,0,0,5,93,0,0,0,0,5,93,42,224,26,207,37,138,107,57,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0]","payloadType":"bin","x":320,"y":820,"wires":[["86da72d0ea660d34"]]},{"id":"1fb0717a014c6dd5","type":"debug","z":"1b5ed5b363954ae7","name":"Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":880,"wires":[]},{"id":"86da72d0ea660d34","type":"buffer-parser","z":"1b5ed5b363954ae7","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"uint16be","name":"PrdWt","offset":6,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nOff","offset":8,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nPChg","offset":10,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nCChg","offset":12,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nOvrL","offset":13,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"nEOff","offset":16,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"ovrTl","offset":18,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"winTl","offset":20,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"blwTl","offset":22,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"tPrdOK","offset":24,"length":1,"offsetbit":0,"scale":"1","mask":""},{"type":"uint16be","name":"DPvalue","offset":26,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"swap16","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"keyvalue","resultTypeType":"return","multipleResult":false,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":510,"y":820,"wires":[["f53d51a2895034b0"]]},{"id":"f53d51a2895034b0","type":"function","z":"1b5ed5b363954ae7","name":"Populate Time and Device_ID","func":"msg.payload.Device_ID = msg.topic.split('/')[0]\nmsg.payload.Time = new Date().toISOString()\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":820,"wires":[["1fb0717a014c6dd5"]]}]

```

NOTES:

- This demo requires you to install [`node-red-contrib-buffer-parser`](https://flows.nodered.org/node/node-red-contrib-buffer-parser)
- You may need to UN-swap the data and use the `LE` (little endian) versions e.g. UINT16LE instead of UINT16BE
- Remember, that manual _COULD BE_ incorrect (it isn't exactly high quality!)

---

<div class="post-metadata">

**Author:** ![shri360](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shri360/32/37341_2.png) [@shri360](https://discourse.nodered.org/u/shri360)\
**Post date:** [11 December 2022 10:51 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/12 "2022-12-11T10:51:13Z")

</div>

Thank you so much !!.

Getting the data, i"l cross verify the values with device.

once again thank you.!! closing.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 December 2022 10:51 UTC](https://discourse.nodered.org/t/convert-buffer-to-proper-object/72020/13 "2022-12-25T10:51:59Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
