Determining if value inside msg.payload is an array

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:

[{"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:

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

And here's the code inside the function node:

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.

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.

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

"Array.isArray() - JavaScript | MDN" Array.isArray() - JavaScript | MDN

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:

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

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.

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:

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");

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).

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:

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?

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...

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

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!

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!

1 Like