# Converting msg.payload array into an object to access its value

**URL:** <https://discourse.nodered.org/t/converting-msg-payload-array-into-an-object-to-access-its-value/68905>\
**Category:** General\
**Created:** [12 October 2022 06:49 UTC](https://discourse.nodered.org/t/converting-msg-payload-array-into-an-object-to-access-its-value/68905 "2022-10-12T06:49:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vedant](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@Vedant](https://discourse.nodered.org/u/Vedant)\
**Post date:** [12 October 2022 06:49 UTC](https://discourse.nodered.org/t/converting-msg-payload-array-into-an-object-to-access-its-value/68905/1 "2022-10-12T06:49:46Z")

</div>

Hello everyone!  
Greetings of the day! I am currently stuck on a pronlem and need your expert opinion regarding the same. I want to convert a msg.payload which is an array into an object as I want to access a value from that array and set it to a different node. Can you please help me regarding the same. I am attaching the pictures for your reference.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/4/8429316b72618d7e473bce63df9dcfd1e2b09794.png)

I want to access the value in valueread. Any help is highly appreciated!  
Thank you

---

<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:** [12 October 2022 07:31 UTC](https://discourse.nodered.org/t/converting-msg-payload-array-into-an-object-to-access-its-value/68905/2 "2022-10-12T07:31:02Z")

</div>

1. search & replace `'` with `"` in msg.payload[0]
2. copy msg.payload[0] to msg.payload
3. pass it through a JSON node

↑ This assumes there is only 1 in element [0] (thats all you show)

PS  
Please post data instead of pictures (I would have made you a quick working demo)

* * *

#### TIPS

To copy data from node-red ALWAYS use the "copy value" button that appears when you hover over the property

When pasting data, ALWAYS press the code button `</>` first on the forum toolbar

---

<div class="post-metadata">

**Author:** ![Vedant](https://avatars.discourse-cdn.com/v4/letter/v/ba9def/32.png) [@Vedant](https://discourse.nodered.org/u/Vedant)\
**Post date:** [12 October 2022 07:42 UTC](https://discourse.nodered.org/t/converting-msg-payload-array-into-an-object-to-access-its-value/68905/3 "2022-10-12T07:42:16Z")

</div>

Hey, thank you for your quick response. The payload is -

```auto
"{'connection': 'open', 'data_status': 'Data recieved from the controller', 'service': '8e', 'status': '00', 'addedstatussize': '00', 'addedstatus': '0000', 'valueread': '1', 'connection_status': 'closed'}"

```

I have formátted the data like you said but that is in python, once the data is recieved from the controller and displayed in node-red it automatically converts " to '  
json= {"connection": "open" , "data\_status":"Data recieved from the controller", "service": data[24:25].hex(), "status": data[25:26].hex(), "addedstatussize": data[26:27].hex(), "addedstatus": data[29:30].hex() + data[28:29].hex(), "valueread": str(int.from\_bytes(data[32:33], "big"),),"connection\_status": "closed" }  
print(json) # printing the data that the controller sends

_admin edit - format data as instructed in previous post!_

---

<div class="post-metadata">

**Author:** ![cameo69](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cameo69/32/66593_2.png) [@cameo69](https://discourse.nodered.org/u/cameo69)\
**Post date:** [12 October 2022 07:42 UTC](https://discourse.nodered.org/t/converting-msg-payload-array-into-an-object-to-access-its-value/68905/4 "2022-10-12T07:42:36Z")

</div>

Hi, next time you might wanna put the actual text rather than an image in the post.

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

```auto
[{"id":"6941a25e8d7159af","type":"inject","z":"46bc23ae0f413065","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"{'connection': 'open','data status': 'Data recieved from the controller','service': 'Be','status':'00','addedstatussize': '00','addedstatus': '0000','valueread': '1','connection status':'closed'}\"]","payloadType":"json","x":230,"y":240,"wires":[["c65749d5d7994507"]]},{"id":"0b2fab7daa98d303","type":"json","z":"46bc23ae0f413065","name":"","property":"payload","action":"","pretty":false,"x":590,"y":240,"wires":[["dbb99aabc2aac8b5","a0450d3b18150beb"]]},{"id":"c65749d5d7994507","type":"change","z":"46bc23ae0f413065","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[0]","tot":"msg"},{"t":"change","p":"payload","pt":"msg","from":"'","fromt":"str","to":"\"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":240,"wires":[["0b2fab7daa98d303"]]},{"id":"dbb99aabc2aac8b5","type":"debug","z":"46bc23ae0f413065","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":280,"wires":[]},{"id":"a0450d3b18150beb","type":"debug","z":"46bc23ae0f413065","name":"valueread","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.valueread","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":240,"wires":[]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/861633aa5a4860c46c0d9f1117fa91e91d4e1718.png)

---

<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:** [26 October 2022 07:42 UTC](https://discourse.nodered.org/t/converting-msg-payload-array-into-an-object-to-access-its-value/68905/5 "2022-10-26T07:42:44Z")

</div>

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