Mustache template and iterating over an array of objects

Hi,
I'm trying to iterate over an array of JSON objects, with the template node and mustache templates (because I need to parse it to XML eventually).
For some reason though the keys return empty, and that I don't get back an array.

This is my flow:

[{"id":"6719f4f2.86966c","type":"inject","z":"8223ec9c.e8765","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"ORDERID\":\"PO20000005\",\"NOTES\":null,\"CREATEDATE\":\"2020-04-21T00:00:00+03:00\",\"PORDERITEMS\":[{\"ORDERLINE\":66,\"EXPECTEDDATE\":null,\"SKU\":\"abc\",\"QTYORDERED\":10,\"MNFPARTNO\":null},{\"ORDERLINE\":67,\"EXPECTEDDATE\":null,\"SKU\":\"def\",\"QTYORDERED\":11,\"MNFPARTNO\":null}]}","payloadType":"json","x":400,"y":540,"wires":[["14abf52a.65d51b"]]},{"id":"14abf52a.65d51b","type":"template","z":"8223ec9c.e8765","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"DATA\": {\n        \"CONSIGNEE\": [\n            \"123\"\n        ],\n        \"ORDERID\": [\n            \"{{{PORDERS.ORDERID}}}\"\n        ],\n        \"ORDERTYPE\": [\n            \"PO\"\n        ],\n        \"REFERENCEORD\": [\n            \"123\"\n        ],\n        \"SOURCECOMPANY\": [\n            \"100\"\n        ],\n        \"COMPANYTYPE\": [\n            \"123\"\n        ],\n        \"NOTES\": [\n            \"{{{PORDERS.NOTES}}}\"\n        ],\n        \"CREATEDATE\": [\n            \"{{{PORDERS.CREATEDATE}}}\"\n        ],\n        \"LINES\": {\n        {{{PORDERS.#PORDERITEMS}}}\n            \"LINE\": {\n                \"ORDERLINE\": [\n                    \"{{{ORDERLINE}}}\"\n                ],\n                \"REFERENCEORDLINE\": [\n                    \"2\"\n                ],\n                \"EXPECTEDDATE\": [\n                    \"{{{EXPECTEDDATE}}}\"\n                ],\n                \"SKU\": [\n                    \"{{{SKU}}}\"\n                ],\n                \"INVENTORYSTATUS\": [\n                    \"HOLD-IQC\"\n                ],\n                \"QTYORDERED\": [\n                    \"{{{QTYORDERED}}}\"\n                ],\n                \"MNFVALID\": [\"\"],\n                \"MNFPARTNO\": [\n                    \"{{{MNFPARTNO}}}\"\n                ],\n                \"HZRD\": [\"\"]\n            }\n         {{{/}}}\n        }\n    }\n}","output":"json","x":560,"y":540,"wires":[["eeb76c21.cfdba","a3cebab6.a58b78"]]},{"id":"eeb76c21.cfdba","type":"debug","z":"8223ec9c.e8765","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":540,"wires":[]},{"id":"a3cebab6.a58b78","type":"xml","z":"8223ec9c.e8765","name":"","property":"payload","attr":"","chr":"","x":710,"y":500,"wires":[["eeb76c21.cfdba"]]}]

My goal is that LINES will be an array with two objects, each for the PORDERITEMS item.

I would like some help understanding how to fix it.
Thanks!

Is this the final format?

<DATA>
    <CONSIGNEE>123</CONSIGNEE>
    <ORDERID>PO20000005</ORDERID>
    <ORDERTYPE>PO</ORDERTYPE>
    <REFERENCEORD>123</REFERENCEORD>
    <SOURCECOMPANY>100</SOURCECOMPANY>
    <COMPANYTYPE>123</COMPANYTYPE>
    <NOTES/>
    <CREATEDATE>2020-04-21T00:00:00+03:00</CREATEDATE>
    <LINES>
        <LINE>
            <ORDERLINE>66</ORDERLINE>
            <REFERENCEORDLINE>2</REFERENCEORDLINE>
            <EXPECTEDDATE/>
            <SKU>abc</SKU>
            <INVENTORYSTATUS>HOLD-IQC</INVENTORYSTATUS>
            <QTYORDERED>10</QTYORDERED>
            <MNFVALID/>
            <MNFPARTNO/>
            <HZRD/>
        </LINE>
        <LINE>
            <ORDERLINE>67</ORDERLINE>
            <REFERENCEORDLINE>2</REFERENCEORDLINE>
            <EXPECTEDDATE/>
            <SKU>def</SKU>
            <INVENTORYSTATUS>HOLD-IQC</INVENTORYSTATUS>
            <QTYORDERED>11</QTYORDERED>
            <MNFVALID/>
            <MNFPARTNO/>
            <HZRD/>
        </LINE>
    </LINES>
</DATA>

If so, then this will do it (no template though)...

If not - then it might get you closer to a solution.

Import this flow if its of any use to you...

[{"id":"cec9c0ce.0ed32","type":"inject","z":"5a83b5d5.d4161c","name":"","topic":"","payload":"{\"ORDERID\":\"PO20000005\",\"NOTES\":null,\"CREATEDATE\":\"2020-04-21T00:00:00+03:00\",\"PORDERITEMS\":[{\"ORDERLINE\":66,\"EXPECTEDDATE\":null,\"SKU\":\"abc\",\"QTYORDERED\":10,\"MNFPARTNO\":null},{\"ORDERLINE\":67,\"EXPECTEDDATE\":null,\"SKU\":\"def\",\"QTYORDERED\":11,\"MNFPARTNO\":null}]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":260,"wires":[["f60807d9.8986a8"]]},{"id":"a7b693e5.16c93","type":"debug","z":"5a83b5d5.d4161c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":780,"y":260,"wires":[]},{"id":"a861792a.4703e8","type":"xml","z":"5a83b5d5.d4161c","name":"","property":"payload","attr":"","chr":"","x":610,"y":260,"wires":[["a7b693e5.16c93"]]},{"id":"f60807d9.8986a8","type":"function","z":"5a83b5d5.d4161c","name":"prepare for XML","func":"\nvar result = {\n    \"DATA\": {\n        \"CONSIGNEE\": [\n            \"123\"\n        ],\n        \"ORDERID\": [msg.payload.ORDERID],\n        \"ORDERTYPE\": [\n            \"PO\"\n        ],\n        \"REFERENCEORD\": [\n            \"123\"\n        ],\n        \"SOURCECOMPANY\": [\n            \"100\"\n        ],\n        \"COMPANYTYPE\": [\n            \"123\"\n        ],\n        \"NOTES\": [ msg.payload.NOTES ],\n        \"CREATEDATE\": [ msg.payload.CREATEDATE ],\n        \"LINES\": []\n    }\n}\n\nvar lines =  msg.payload.PORDERITEMS.map(e => {\n    return {\n        LINE :\n        {  \n            ORDERLINE :  [ e.ORDERLINE],\n            REFERENCEORDLINE: [\"2\"],\n            EXPECTEDDATE: [ e.EXPECTEDDATE],\n            SKU: [ e.SKU] ,\n            INVENTORYSTATUS: [\"HOLD-IQC\"],\n            QTYORDERED: [ e.QTYORDERED ],\n            MNFVALID: [\"\"],\n            MNFPARTNO: [ e.MNFPARTNO ],\n            HZRD: [\"\"]\n        }\n    }\n});\nresult.DATA.LINES.push(lines);\nmsg.payloadJS = result;\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":260,"wires":[["a861792a.4703e8"]]}]
1 Like

Thanks, this is perfect.

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