In my Node-RED function, I've been struggling to handle the msg.payload and extract a specific component of the json involved with it.
Here's the code
var js = msg.payload;
var output = {};
//var Ids = js.events.sum;//.value;
//var labels = js.externalIds.Id;
js.forEach(function(sum){
output.push(js.events.sum);
})
msg.payload = output;
console.log(msg.payload);
return msg;
I've tried store a "JSON.parse()"-d variable of the msg and msg.payload, but each time I received an 'unexpected o' error, which I've been informed means something was parsed redundantly.
I've been looking at
forEach is not a function error with JavaScript array
forEach is not a function error
SyntaxError: Unexpected token o in JSON at position 1
But these haven't clarified how to handle msg objects.
The following have also been helpful for other causes but not for figuring out why my js variable cannot 'foreach':
https://nodered.org/docs/user-guide/messages
https://nodered.org/docs/user-guide/writing-functions
The exact error is TypeError: js.forEach is not a function
Clarification appreciated.
Edit
I'm trying to select data from an api call that returns JSON of multiple instances of the same types data (different values). From those instances of the same types, I'm trying to select the returned values of one type.
For example,
Let's say the returned json is structured like this:
{
"tupperware": [
{
"spoon": "salad spoon",
"fork": "soup fork"
},
{
"spoon": "soup spoon",
"fork": "cake fork"
}
],
}
And I'm trying to select only the fork values. How would I go about doing that?