JSON array parse

Hi all,

I'm fairly new to Node-RED and totally unfamiliar with javascript. Who can help me with my code?

I'm trying to parse a WooCommerce webhook and send some information to my phone thru join (joaoapps).

I searched the web for solutions and I think i'm almost there. I found this example on stackoverflow and hacked up this little code on https://js.do/code/json_iteration.
This displays the result what i'm looking for.

When i'm trying to translate this into Node-RED, i'm only getting the last part (Book D) of the array.
Here's my flow code:

[
    {
        "id": "cc4c4710.0e42f8",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "6b97b4d9.508d9c",
        "type": "inject",
        "z": "cc4c4710.0e42f8",
        "name": "Test JSON inject",
        "topic": "",
        "payload": "{\"status\":\"completed\",\"total\":\"80.50\",\"line_items\":[{\"name\":\"Book A\",\"quantity\":1},{\"name\":\"Book B\",\"quantity\":2},{\"name\":\"Book C\",\"quantity\":3},{\"name\":\"Book D\",\"quantity\":4}]}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 240,
        "y": 160,
        "wires": [
            [
                "71becfa3.e7d4b"
            ]
        ]
    },
    {
        "id": "71becfa3.e7d4b",
        "type": "switch",
        "z": "cc4c4710.0e42f8",
        "name": "",
        "property": "payload.status",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "completed",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 250,
        "y": 260,
        "wires": [
            [
                "b082823.37b478"
            ]
        ]
    },
    {
        "id": "b082823.37b478",
        "type": "function",
        "z": "cc4c4710.0e42f8",
        "name": "Message format",
        "func": "var arr = msg.payload.line_items;\n\nfor (var i = 0; i < arr.length; i++){\n    var obj = arr[i];\n    book = (\"\\n What: \" + obj.name);\n    amount = (\"\\n How many: \" + obj.quantity);\n}\n\nmoney = msg.payload.total;\nmsg.title = \"Books sold!\";\nmsg.text = book + amount + \"\\n\\n Total money earned: \" + money\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 260,
        "y": 360,
        "wires": [
            [
                "49943e66.fc353"
            ]
        ]
    },
    {
        "id": "49943e66.fc353",
        "type": "debug",
        "z": "cc4c4710.0e42f8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 270,
        "y": 460,
        "wires": []
    }
]

The WooCommerce JSON output is redacted of course, and the switch node is there to only display when a order is completed.

I think my for loop is wrong because of my book and amount variable being overwritten by each iteration :thinking:

But how can I fix this? Just thinking out loud here :upside_down_face:

In the loop, create your output

I'm a bit further by putting the values in a new array.

var arr = msg.payload.line_items;
var newMsg = [];
for (var i = 0; i < arr.length; i++){
    var obj = arr[i];
    newMsg.push("\n What: " + obj.name + "\n How many: " + obj.quantity);
}

money = msg.payload.total;
msg.title = "Books sold!";
msg.text = newMsg;

return msg;

This will show all the books in the msg.text but I think join doesn't support a object or array. So now I need to turn it into a string to work I guess. :thinking:

try this

var arr = msg.payload.line_items;
var output = ''
for (var i = 0; i < arr.length; i++){
    var obj = arr[i];
    output += ("\n What: " + obj.name);
    output += ("\n How many: " + obj.quantity);
}

money = msg.payload.total;
msg.title = "Books sold!";
output += "\n\n Total money earned: " + money
msg.text = output

return msg;

I think I got it fixed :smiley:

I converted newMsg.toString() and now it shows up on join

Working code here:

var arr = msg.payload.line_items;
var newMsg = [];
for (var i = 0; i < arr.length; i++){
    var obj = arr[i];
    newMsg.push("\n What: " + obj.name + "\n How many: " + obj.quantity);
}

var convMsg = newMsg.toString();
money = msg.payload.total;
msg.title = "Books sold!";
msg.text = convMsg;

return msg;

If there's a better or easier way, please do tell. For now i'm excited it works.
Sorry for thinking out loud here :crazy_face:

Sorry just saw your proposal now. Will test that as well :slightly_smiling_face:

There is always multiple ways to solve a problem :grin:

I chose your solution because it looks neater and no conversion toString is needed :wink:

Thank you for looking at my code and your proposal :+1:t3:

P.S. I marked your reply as the solution

2 Likes

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