Split in Batches Strange Behavior

Hello.

Can someone explain me and tell me why it behaves like that split in batces node according to the following scenario?

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -1490,
        -1400
      ]
    },
    {
      "parameters": {
        "functionCode": "let my_array = [\n{\n\"Customers\": \"bags\"\n},\n{\n\"Customers\": \"alic\"\n},\n{\n\"Customers\": \"aega\"\n},\n{\n\"Customers\": \"txmf\"\n}\n] \n\nreturn my_array.map(e => { return { json: e } });"
      },
      "name": "Example Array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -1190,
        -1390
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Customer",
              "value": "={{$node[\"Split1\"].json[\"Customers\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -270,
        -1770
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split2",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        -590,
        -1480
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split3",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        -650,
        -1030
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Customer",
              "value": "={{$node[\"Split2\"].json[\"Customers\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -280,
        -1240
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Customer",
              "value": "={{$node[\"Split3\"].json[\"Customers\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set3",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -220,
        -1030
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split1",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        -570,
        -1770
      ]
    },
    {
      "parameters": {},
      "name": "NoOp3",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        -430,
        -1030
      ]
    },
    {
      "parameters": {},
      "name": "NoOp4",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        -420,
        -780
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Customer",
              "value": "={{$node[\"Split4\"].json[\"Customers\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set4",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -180,
        -500
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split4",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        -650,
        -780
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Customer",
              "value": "={{$node[\"Split4\"].json[\"Customers\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set5",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        120,
        -500
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Example Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Example Array": {
      "main": [
        [
          {
            "node": "Split1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split2",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split3",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split2": {
      "main": [
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split3": {
      "main": [
        [
          {
            "node": "NoOp3",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set2": {
      "main": [
        [
          {
            "node": "Split2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split1": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "NoOp3": {
      "main": [
        [
          {
            "node": "Set3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "NoOp4": {
      "main": [
        [
          {
            "node": "Set4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set4": {
      "main": [
        [
          {
            "node": "Split4",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set5",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split4": {
      "main": [
        [
          {
            "node": "NoOp4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

You can see if you run it that the nodes Set3 and Set5 produces wrong results.

Why is that happening if there is an intermediate node before the loop point and the last 2 results of these nodes are duplicates?

Aren’t suppose to have all Set nodes the same results?

In your example, Split3 loops to itself before the item gets passed on. In Set3 you are then not looking at the incoming data but at the value the corresponding Item from Split3. If you simply change the expression used in Set3 from {{$node["Split3"].json["Customers"]}} to {{$json["Customers"]}} to use the actual incoming data rather than whatever Split3 is currently looking at, you would have the desired output.

This also goes for Set5, you aren’t using the data coming in in Set5 but are referencing data from Split4 (which loops to the next item before Set5 is reached).

So just looking at the incoming items at each of your Set nodes (= using $json instead of $node) would avoid such trouble.

On a more general note, looping is often not required and complicates things a lot. So it’s always good to keep this list in mind.

1 Like

Thank you very much for your explanation.

Now i understand the logic and i will try to fix my other workflows too.

1 Like

Glad to hear this helps, thanks a lot for confirming!

On a side node, the input data can also be added through the expression editor, so you don’t have to type it manually. You can find it under Current Node > Input Data > JSON:

Hope this helps :slight_smile:

@MutedJam

Can you look this example:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "let my_array = [\n{\n\"Customers\": \"bags\"\n},\n{\n\"Customers\": \"alic\"\n},\n{\n\"Customers\": \"aega\"\n},\n{\n\"Customers\": \"txmf\"\n}\n] \n\nreturn my_array.map(e => { return { json: e } });"
      },
      "name": "Example Array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -1190,
        -1390
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Customer",
              "value": "={{$node[\"Split\"].json[\"Customers\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -260,
        -1390
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "boolean": [
            {
              "name": "Test",
              "value": true
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -570,
        -1390
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        -850,
        -1390
      ]
    }
  ],
  "connections": {
    "Example Array": {
      "main": [
        [
          {
            "node": "Split",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

What can you do when you have an intermidiate node that produces different data?

How can you get the correct data from Split Batch then?

You have to start the loop from the Set2 or else there is not other way?

Well, in this example a loop wouldn’t be required - the set nodes would run once for each input item even without Split in Batches:

But assuming you do need a loop, I would indeed loop after your Set2 node as you have suggested:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "let my_array = [\n{\n\"Customers\": \"bags\"\n},\n{\n\"Customers\": \"alic\"\n},\n{\n\"Customers\": \"aega\"\n},\n{\n\"Customers\": \"txmf\"\n}\n] \n\nreturn my_array.map(e => { return { json: e } });"
      },
      "name": "Example Array",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Customer",
              "value": "={{$node[\"Split\"].json[\"Customers\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "boolean": [
            {
              "name": "Test",
              "value": true
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "Split",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        650,
        450
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Example Array",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Example Array": {
      "main": [
        [
          {
            "node": "Split",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set2": {
      "main": [
        [
          {
            "node": "Split",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thank you.

Really appreciate it.

1 Like