# Determining if value inside msg.payload is an array

**URL:** <https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479>\
**Category:** General\
**Tags:** function-node\
**Created:** [11 January 2022 17:05 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479 "2022-01-11T17:05:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jdo300](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdo300/32/79415_2.png) [@Jdo300](https://discourse.nodered.org/u/Jdo300)\
**Post date:** [11 January 2022 17:05 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/1 "2022-01-11T17:05:35Z")

</div>

So I'm trying to take an input JSON object which contains an array of values and simply determine, in my function node, if the variable contains an array or not. It works fine if I declare the object directly inside the function node and do the check on it. But if I inject the object as JSON and convert to to an object with the JSON node, it fails the check. Printing both objects out to the debug window indicates they are identical so I'm not sure what I'm doing wrong here. Here's an example flow illustrating the problem:

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

```auto
[{"id":"c55f8d246ea89274","type":"inject","z":"36608fa567377833","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"anArray\":[1,2,3]}","payloadType":"json","x":150,"y":2180,"wires":[["c8a96f421a679e11"]]},{"id":"c8a96f421a679e11","type":"json","z":"36608fa567377833","name":"","property":"payload","action":"obj","pretty":false,"x":330,"y":2180,"wires":[["178e7f23207c8603"]]},{"id":"178e7f23207c8603","type":"function","z":"36608fa567377833","name":"","func":"var object1 = {\n anArray: [1, 2, 3]\n}\n\nnode.warn(object1);\nnode.warn(msg.payload);\n\nif (object1.anArray instanceof Array)\n node.warn(\"Is Array\");\nelse\n node.warn(\"Is Not Array\");\n \nif (msg.payload.anArray instanceof Array)\n node.warn(\"Is Array\");\nelse\n node.warn(\"Is Not Array\");\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":2180,"wires":[["1b83731fc29d42d4"]]},{"id":"1b83731fc29d42d4","type":"debug","z":"36608fa567377833","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":630,"y":2180,"wires":[]}]

```

Here's the JSON object that I'm injecting into the function node via a JSON node:

```auto
{
    "anArray": [1, 2, 3]
}

```

And here's the code inside the function node:

```auto
var object1 = {
    anArray: [1, 2, 3]
}

node.warn(object1);
node.warn(msg.payload);

if (object1.anArray instanceof Array)
    node.warn("Is Array");
else
    node.warn("Is Not Array");
    
if (msg.payload.anArray instanceof Array)
    node.warn("Is Array");
else
    node.warn("Is Not Array");

```

Any help greatly appreciated.

---

<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 January 2022 17:19 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/2 "2022-01-11T17:19:29Z")

</div>

> [@Jdo300](#):
>
> Here's the JSON object that I'm injecting into the function node via a JSON node:
> 
> ```auto
> {
> "anArray": [1, 2, 3]
> }
> 
> ```

You aren't injecting a JSON object you are injecting a javascript object constructed from the JSON string that you have put in the inject node. Then the JSON node converts it to a string so the function fails.

---

<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 January 2022 17:30 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/3 "2022-01-11T17:30:32Z")

</div>

If you want to check in a function, use `Array.isArray`

"Array.isArray() - JavaScript | MDN" [Array.isArray() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [11 January 2022 18:03 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/4 "2022-01-11T18:03:55Z")

</div>

FYI, here are a couple other ways to check if the payload is an array...

using a `change` node expression to return a true/false value:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/b/eba4b0202f6fb5af70f2563a55627d677a65ad1f.png)

or using a `switch` node to route to different output ports:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/32bb060086d53467770ea61e1aec7a4e269800ba.png)

---

<div class="post-metadata">

**Author:** ![Jdo300](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdo300/32/79415_2.png) [@Jdo300](https://discourse.nodered.org/u/Jdo300)\
**Post date:** [12 January 2022 14:42 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/5 "2022-01-12T14:42:44Z")

</div>

Thank you for the feedback, Colin. I wasn't sure if the output was an object or string, so I set the JSON node to always convert to an object just in case. I did remove the JSON node and just feed it directly to the function but I still get the same result.

---

<div class="post-metadata">

**Author:** ![Jdo300](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdo300/32/79415_2.png) [@Jdo300](https://discourse.nodered.org/u/Jdo300)\
**Post date:** [12 January 2022 14:46 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/6 "2022-01-12T14:46:19Z")

</div>

I did try with the .isArray function (I actually used this first originally when I made the code in a JSFiddle). For some reason, both checks fail (for both the internal object and the one in msg.payload) Here's the code I used:

```auto
if (object1.anArray.isArray)
    node.warn("Is Array");
else
    node.warn("Is Not Array");
    
if (msg.payload.anArray.isArray)
    node.warn("Is Array");
else
    node.warn("Is Not Array");

```

---

<div class="post-metadata">

**Author:** ![Jdo300](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdo300/32/79415_2.png) [@Jdo300](https://discourse.nodered.org/u/Jdo300)\
**Post date:** [12 January 2022 14:48 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/7 "2022-01-12T14:48:50Z")

</div>

Thank you for the suggestion here. I didn't know the change node had that functionality. Unfortunately, I need to do the check inside of a function node (the code sample I posted was a simplified version of a larger function I'm implementing).

---

<div class="post-metadata">

**Author:** ![Jdo300](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdo300/32/79415_2.png) [@Jdo300](https://discourse.nodered.org/u/Jdo300)\
**Post date:** [12 January 2022 14:50 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/8 "2022-01-12T14:50:58Z")

</div>

So I was able to find a workaround that correctly checks both objects, though I'm not sure exactly why it works. If you simply remove the JSON node from the above example and replace the function node's code with the following, it works:

```auto
var object1 = {
    anArray: [1, 2, 3]
}

node.warn(object1);
node.warn(msg.payload);

if (JSON.parse(JSON.stringify(object1.anArray)) instanceof Array)
    node.warn("Is Array");
else
    node.warn("Is Not Array");
    
if (JSON.parse(JSON.stringify(msg.payload.anArray)) instanceof Array)
    node.warn("Is Array");
else
    node.warn("Is Not Array");

```

I'm not sure why this is the case. I don't see where my JSON input object is malformed. Does anyone have any idea why this works?

---

<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 January 2022 15:03 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/9 "2022-01-12T15:03:22Z")

</div>

> [@Jdo300](#):
>
> `if (object1.anArray.isArray)`

That's not how you use it nor is that how it's demoed with a working example in the mdn document I linked to...

> [@Steve-Mcl](#):
>
> use `Array.isArray`
> 
> "Array.isArray() - JavaScript | MDN" [Array.isArray() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)

In your case it would be `if (Array.isArray(object1.anArray))`

---

<div class="post-metadata">

**Author:** ![Jdo300](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jdo300/32/79415_2.png) [@Jdo300](https://discourse.nodered.org/u/Jdo300)\
**Post date:** [12 January 2022 15:35 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/10 "2022-01-12T15:35:11Z")

</div>

Oh Wow. I completely misread that example. Thank you for pointing that out. I corrected my code for the example above and now it works perfectly!

```auto
var object1 = {
    anArray: [1, 2, 3]
}

node.warn(object1);
node.warn(msg.payload);

if (Array.isArray(object1.anArray))
    node.warn("Is Array");
else
    node.warn("Is Not Array");
    
if (Array.isArray(msg.payload.anArray))
    node.warn("Is Array");
else
    node.warn("Is Not Array");

```

Thank you!

---

<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 January 2022 15:35 UTC](https://discourse.nodered.org/t/determining-if-value-inside-msg-payload-is-an-array/56479/11 "2022-01-26T15:35:58Z")

</div>

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