Split in branches, 'default' script doesn't work

Below is the JSON i receive from a get post on open orders, this displays a single result, but could be multiple orderId as well:

[
  {
    "orders": [
      {
        "orderId": "1246474783",
        "orderPlacedDateTime": "2021-07-06T15:56:22+02:00",
        "orderItems": [
          {
            "orderItemId": "2686869490",
            "ean": "5902533290666",
            "quantity": 1,
            "quantityShipped": 0,
            "quantityCancelled": 0
          }
        ]
      }
    ]
  }
]

I need to loop my flow over the multiple orders, in case there are multiple orders. I searched the forum and found the split branche example here: JavaScript Code Snippets | Docs

But i see this error:
ERROR: items[0].json.map is not a function

After running this in a function node:

return items[0].json.map(item => {
return {
json: item
}
});

What do i need to do to be able to use split in branches over multiple OrderId’s?

Thank you in advance!

N8N is making us very happy! Able to get (single) order from bol.com platform and drop them in a Magento2 platform all within a couple of hours :slight_smile:

1 Like

Glad that you are enjoying n8n.

In that case, the function node should look like this:

return items[0].json.orders.map((order) => ({ json: order }))
Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: \n    {\n    \"orders\": [\n      {\n        \"orderId\": \"1246474783\",\n        \"orderPlacedDateTime\": \"2021-07-06T15:56:22+02:00\",\n        \"orderItems\": [\n          {\n            \"orderItemId\": \"2686869490\",\n            \"ean\": \"5902533290666\",\n            \"quantity\": 1,\n            \"quantityShipped\": 0,\n            \"quantityCancelled\": 0\n          }\n        ]\n      }\n    ]\n  }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.orders.map((order) => ({ json: order }))"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        780,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thank you i’ll test as soon as possible with multiple orders!