# How to pull a variable from an array in payload

**URL:** <https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057>\
**Category:** General\
**Created:** [13 July 2022 15:36 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057 "2022-07-13T15:36:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaimmor](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaimmor/32/64555_2.png) [@jaimmor](https://discourse.nodered.org/u/jaimmor)\
**Post date:** [13 July 2022 15:36 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/1 "2022-07-13T15:36:28Z")

</div>

Hi all

Hoping someone can help me. I am trying to pull sensor information from a device (MQTT), but having trouble extracting just what I need from the payload.

Debug output looks like this:

7/13/2022, 10:24:01 AM  
node: 0c1ee5723f948408meraki/v1/mt/L\_585467951558182753/ble/{DeviceMAC}/temperature : msg : Object

object \>  
topic: "meraki/v1/mt/L\_585467951558182753/ble/{DeviceMAC}/temperature"  
payload: "{"ts":"2022-07-13T08:16:37Z","fahrenheit":75.5,"celsius":24.2}"  
qos: 0  
retain: true  
\_msgid: "f62fd05586202cef"

When I go to extract "fahrenheit":75.5, I try to do it like this:  
msg.payload[1] but that only seems to get me """, msg.payload[2] is "t", msg.payload[3] is "s", so it seems that the payload is not an actual array, it seems to be reading it as a string?. Does that make sense?

Any ideas how I can extract any of those 3 values? (timestamp, F, C)

---

<div class="post-metadata">

**Author:** ![HaroldPetersInskipp](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/haroldpetersinskipp/32/42319_2.png) [@HaroldPetersInskipp](https://discourse.nodered.org/u/HaroldPetersInskipp)\
**Post date:** [13 July 2022 15:42 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/2 "2022-07-13T15:42:21Z")

</div>

In this case `msg.payload` is an object (objects are surrounded by curly braces `{}`), not an array (arrays are surrounded by brackets `[]`). You can access them like `msg.payload.ts` or `msg.payload.fahrenheit` rather than `msg.payload[X]`

`msg.payload` could also be a `JSON string`, in which case you would need to convert it into an object first.

---

<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:** [13 July 2022 15:50 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/3 "2022-07-13T15:50:36Z")

</div>

> [@jaimmor](#):
>
> Any ideas how I can extract any of those 3 values? (timestamp, F, C)

Use the built in tools provided...

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![jaimmor](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaimmor/32/64555_2.png) [@jaimmor](https://discourse.nodered.org/u/jaimmor)\
**Post date:** [13 July 2022 15:54 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/4 "2022-07-13T15:54:44Z")

</div>

Thank you for your reply. Sorry, forgot to mention that I had tried that but I get undefined in the debug output

---

<div class="post-metadata">

**Author:** ![jaimmor](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaimmor/32/64555_2.png) [@jaimmor](https://discourse.nodered.org/u/jaimmor)\
**Post date:** [13 July 2022 15:57 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/5 "2022-07-13T15:57:09Z")

</div>

Yeah I think I do need to convert it to an object first, let me figure that out and see what I get

---

<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:** [13 July 2022 15:58 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/6 "2022-07-13T15:58:16Z")

</div>

It looks like payload is a string.

Are you outputting an object from the mqtt node? If not select output a parse object in the mqtt node config.  
Then fahrenheit will be an object property, You will then access it at `msg.payload.fahrenheit`

---

<div class="post-metadata">

**Author:** ![jaimmor](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaimmor/32/64555_2.png) [@jaimmor](https://discourse.nodered.org/u/jaimmor)\
**Post date:** [13 July 2022 16:08 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/7 "2022-07-13T16:08:10Z")

</div>

Great callout! Didnt even think to look at what my mqtt node was configured as, it was set to automatic (string or buffer)  
I changed it to JSON object, and now I can call on it using msg.payload.fahrenheit

Thank you all for your help and guidance! As you can tell, I am a newbie, but love learning new things 🙂

---

<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:** [27 July 2022 16:08 UTC](https://discourse.nodered.org/t/how-to-pull-a-variable-from-an-array-in-payload/65057/8 "2022-07-27T16:08:12Z")

</div>

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