# How do I pull values out of an array of objects

**URL:** <https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520>\
**Category:** General\
**Tags:** http-request\
**Created:** [1 September 2021 22:30 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520 "2021-09-01T22:30:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [1 September 2021 22:30 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/1 "2021-09-01T22:30:03Z")

</div>

Hey All, Not to sure exactly how to go about this. Been spending a good part of the week getting  
atleast this far and was wondering if anyone knew how to pull out values from an item within an  
object which is within an array. Its coming out of a get request for my inverter and id like to display  
the values in a dashboard.

Im using 2 split nodes and a switch node to get the data for my inverter using the site ID to get the  
right object. I have used a debug to drill down to the value i want but I dont know how to pull the value  
out of the object. Any help would be appreciated . I would really like to be able to get the Voltage value for instance under raw value.

Here is some pictures though and will put the output into a text file. I wish I knew more about Json or  
Java as id been told i could probably pull everything out through a function node... One day I will get  
there.

Thankyou anyone willing to help  
[Output.txt](https://discourse.nodered.org/uploads/short-url/3V0k41FlFNYvMj6wbOQlHS9x51D.txt) (8.3 KB)

 ![2021-09-01 (6)_LI](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/eb6e233cd45bb0bdf3e741ae507474d56c2dfca6.jpeg)  
 ![2021-09-01 (8)_LI](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4ee336bf118bd404fa8daa0b1cfbfb7bf74e8c52.jpeg)

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [1 September 2021 23:56 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/2 "2021-09-01T23:56:53Z")

</div>

Some news is finally made a semi breakthrough? its not ideal but it will work and that is i will have to have multiple function nodes with a similar text to pull all my inverter information. Here is the code for the function node.

```auto
let BV = msg.payload.extended[2].rawValue;
msg.payload = BV;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)\
**Post date:** [2 September 2021 07:11 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/3 "2021-09-02T07:11:23Z")

</div>

Hi!

I'm not sure if I understood what you want to achieve. If you just want all rawValues from your JSON inone array, use a function node with:

```auto
let pe = msg.payload.extended;
let e = pe.map(x => x.rawValue);
msg.payload = e;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [2 September 2021 09:49 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/4 "2021-09-02T09:49:32Z")

</div>

Hi Simon and thanks for the reply,

Not really that way, But i have a number of data values i'm interested in.  
with their own differential paths being something like.

```auto
let GrAlarmText = msg.payload.extended[19].textValue;
let BChargSt = msg.payload.extended[3].rawValue;
let Bchargtex = msg.payload.extended[5].textValue;
let ALR1 = msg.payload.alarm;

```

I tried an approach which where I would have multiple outputs from a function node  
to feed the home dashboard.but with my understanding that didn't work out.  
I tried something like this.

```auto
msg.payload = ALR1;
   return [null, msg];
msg.payload = GrAlarmText;
   return [msg, null];
;

```

This did not work. So I am currently using maybe 60 function nodes lol for each break  
off message I plan to use.

---

<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:** [2 September 2021 10:11 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/5 "2021-09-02T10:11:12Z")

</div>

> [@Jonathan95](#):
>
> I tried an approach which where I would have multiple outputs from a function node  
> to feed the home dashboard

If you want to display an attribute from a message in the dashboard, most dashboard nodes allow you to put, in the Value Format field something like  
`{{msg.payload.extended[3].rawValue}}`  
Then you can feed the full message to all dashboard nodes.

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [2 September 2021 10:20 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/6 "2021-09-02T10:20:28Z")

</div>

Oh no way.... I have got to try this hahahaha. do some other nodes operate the same way?  
Like for instance switch and change nodes? There is this awesome little UI someone here made  
that Ive been using like a traffic light and needs to be fed color changes which then i use a change  
node to act on content of a message.

Thank you for the information. this should save a fair bit on node usage

---

<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:** [2 September 2021 10:33 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/7 "2021-09-02T10:33:01Z")

</div>

> [@Jonathan95](#):
>
> do some other nodes operate the same way?  
> Like for instance switch and change nodes?

Can you give an example of exactly what you want to do in those nodes?

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [2 September 2021 10:46 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/8 "2021-09-02T10:46:54Z")

</div>

Well right now its something like this, With the logics being that it looks for the Grid ok/lost,  
But if the battery is low it should show amber , I wish I knew how to add the next level of logic  
which is that if grid is lost and battery is low then send red . any ideas? The battery percentage  
and the Grid state come from 2 separate function nodes at the moment

```auto
[
    {
        "id": "31643f00.8068a2",
        "type": "ui_template",
        "z": "3b34e0bec79715e3",
        "group": "19d977f5.9091e",
        "name": "Status",
        "order": 4,
        "width": 2,
        "height": 1,
        "format": "<style>\n.led-green {\nmargin: 0 auto;\nwidth: 24px;\nheight: 24px;\nbackground-color: #ABFF00;\nborder-radius: 50%;\n{\n</style>\n<table>\n<tr>\n<td><div class=\"led-green\" style=\"background-color:{{msg.payload}}\"></div></td>\n<td></td>\n</tr>\n</table>\n",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": false,
        "templateScope": "local",
        "x": 1430,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "7d926e81.1c9fb",
        "type": "change",
        "z": "3b34e0bec79715e3",
        "name": "change to red",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "red",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1400,
        "y": 180,
        "wires": [
            [
                "31643f00.8068a2"
            ]
        ]
    },
    {
        "id": "eba70a4.b228078",
        "type": "switch",
        "z": "3b34e0bec79715e3",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "Grid ok",
                "vt": "str"
            },
            {
                "t": "lt",
                "v": "40",
                "vt": "num"
            },
            {
                "t": "cont",
                "v": "Grid lost",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 1070,
        "y": 140,
        "wires": [
            [
                "e0c5f60a.79228"
            ],
            [
                "ab956068591ffd7b"
            ],
            [
                "7d926e81.1c9fb"
            ]
        ]
    },
    {
        "id": "5c433262.392e74",
        "type": "change",
        "z": "3b34e0bec79715e3",
        "name": "change to orange",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "orange",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1410,
        "y": 140,
        "wires": [
            [
                "31643f00.8068a2"
            ]
        ]
    },
    {
        "id": "e0c5f60a.79228",
        "type": "change",
        "z": "3b34e0bec79715e3",
        "name": "change to green",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "green",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1410,
        "y": 100,
        "wires": [
            [
                "31643f00.8068a2"
            ]
        ]
    },
    {
        "id": "ab956068591ffd7b",
        "type": "delay",
        "z": "3b34e0bec79715e3",
        "name": "",
        "pauseType": "delay",
        "timeout": "100",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "x": 1210,
        "y": 140,
        "wires": [
            [
                "5c433262.392e74"
            ]
        ]
    },
    {
        "id": "ddc48e96.3454c8",
        "type": "function",
        "z": "3b34e0bec79715e3",
        "name": "",
        "func": "let GrAlarmText = msg.payload.extended[19].textValue;\nmsg.payload = GrAlarmText;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 800,
        "y": 120,
        "wires": [
            [
                "9ca58e3e.16a668",
                "eba70a4.b228078"
            ]
        ]
    },
    {
        "id": "8e6ac797.8ce97",
        "type": "function",
        "z": "3b34e0bec79715e3",
        "name": "",
        "func": "let BChargSt = msg.payload.extended[3].rawValue;\nmsg.payload = BChargSt;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 800,
        "y": 160,
        "wires": [
            [
                "1d6fb8b3.ce6dc7",
                "eba70a4.b228078"
            ]
        ]
    },
    {
        "id": "19d977f5.9091e",
        "type": "ui_group",
        "name": "CC-Oakhurst-Rondebosch",
        "tab": "3ceee6af.390d22",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "3ceee6af.390d22",
        "type": "ui_tab",
        "name": "Cash Crusaders Overview",
        "icon": "business",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

```

---

<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:** [2 September 2021 10:55 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/9 "2021-09-02T10:55:27Z")

</div>

Replace the function nodes with Switch nodes. Either in parallel or feed one into the other dependant on what you are trying to achieve. In the Switch node you can set the Property field to any property of the message that you want. For example `msg.payload.extended[19].textValue`

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [2 September 2021 11:14 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/10 "2021-09-02T11:14:04Z")

</div>

I will give this a shot thankyou,

What I am looking to get out is that the state is good if both grid connected  
and battery over 50 percent, It must change to amber if batter is below 50 percent and must be red when  
grid is lost.

---

<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:** [2 September 2021 11:32 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/11 "2021-09-02T11:32:48Z")

</div>

Can you rewrite that using the property names please? To save me trying to understand the data.

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [2 September 2021 12:12 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/12 "2021-09-02T12:12:32Z")

</div>

I am going to try , If i understand your question correctly..

This msg gives me grid state which only comes in answes of "Grid ok" or "Grid lost"  
msg.payload.extended[21].textValue

This msg gives me the battery charge Percent which is 0-100  
msg.payload.extended[3].rawValue

This msg gives me the battery state in 3 text string forms "idle" or "charging" or "discharging"  
msg.payload.extended[5].textValue

This msg gives me the power consumption from the grid in a string number from -2000 to 5000  
msg.payload.extended[15].rawValue

I hope this helps

---

<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:** [2 September 2021 12:29 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/13 "2021-09-02T12:29:34Z")

</div>

Is that all in one message?

---

<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:** [2 September 2021 13:04 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/14 "2021-09-02T13:04:21Z")

</div>

If it is all in one message, which I think it is, then I think that can be rewritten as  
If msg.payload.extended[21].textValue is "Grid lost" then set status Red  
Otherwise, if msg.payload.extended[3].rawValue is \<= 50 set status Amber  
Otherwise set status Green.

That can be done using Switch nodes as in this flow.

```auto
[{"id":"12b9fa3ceda3a249","type":"switch","z":"84405ff5.25fa6","name":"","property":"payload.extended[21].textValue","propertyType":"msg","rules":[{"t":"eq","v":"Grid lost","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":190,"y":3160,"wires":[["556fbc5abe4caaf9"],["80c124c13585e360"]]},{"id":"80c124c13585e360","type":"switch","z":"84405ff5.25fa6","name":"","property":"payload.extended[3].rawValue","propertyType":"msg","rules":[{"t":"lte","v":"50","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":350,"y":3200,"wires":[["9028bd9635ca9428"],["fcf4ef05d789e732"]]},{"id":"556fbc5abe4caaf9","type":"change","z":"84405ff5.25fa6","name":"change to red","rules":[{"t":"set","p":"payload","pt":"msg","to":"red","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":3140,"wires":[[]]},{"id":"9028bd9635ca9428","type":"change","z":"84405ff5.25fa6","name":"change to orange","rules":[{"t":"set","p":"payload","pt":"msg","to":"orange","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":3180,"wires":[[]]},{"id":"fcf4ef05d789e732","type":"change","z":"84405ff5.25fa6","name":"change to green","rules":[{"t":"set","p":"payload","pt":"msg","to":"green","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":3220,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![Jonathan95](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jonathan95/32/16370_2.png) [@Jonathan95](https://discourse.nodered.org/u/Jonathan95)\
**Post date:** [2 September 2021 13:47 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/15 "2021-09-02T13:47:13Z")

</div>

Yes you are right it comes from one large message, Sorry for the delay I had to attend to kids 😃 ,  
I see when feeding it from source message it takes it as one and then makes arguments about the  
whole content. I was really thinking that it would get both and split off a message which could have  
overwritten the desired state . Thanks for the insight

---

<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:** [16 September 2021 13:48 UTC](https://discourse.nodered.org/t/how-do-i-pull-values-out-of-an-array-of-objects/50520/16 "2021-09-16T13:48:08Z")

</div>

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