Problem with Nested Split in batches loop workflow - lack of last element

Hi n8n team,

I created this workflow that using nested Split in batches loop. The problem is the SplitInBatches1 does not show the last “113.166.127.111”, instead it uses duplicated value from penultimate value “211.85.221.45”. Can you help me?

{
  "name": "My workflow 3",
  "nodes": [
    {
      "parameters": {},
      "name": "Start2",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -750,
        730
      ]
    },
    {
      "parameters": {
        "functionCode": "item = [\n[\n{\n\"id\": \"85266\",\n\"info\": \"Test 1\",\n\"Attribute\": [\n{\n\"type\": \"ip\",\n\"category\": \"Network\",\n\"value\": \"20.2.95.38\"\n},\n{\n\"type\": \"ip\",\n\"category\": \"Network\",\n\"value\": \"118.69.96.3\"\n},\n{\n\"type\": \"ip\",\n\"category\": \"Network\",\n\"value\": \"220.255.236.153\"\n}\n]\n},\n{\n\"id\": \"87693\",\n\"info\": \"Test 2\",\n\"Attribute\": [\n{\n\"id\": \"5344351\",\n\"type\": \"ip\",\n\"category\": \"Network\",\n\"value\": \"211.85.221.45\"\n},\n{\n\"id\": \"5344354\",\n\"type\": \"ip\",\n\"category\": \"Network\",\n\"value\": \"113.166.127.111\"\n}\n]\n}\n]];\n\nreturn item[0];"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        -450,
        730
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        -130,
        720
      ],
      "notesInFlow": false,
      "alwaysOutputData": false,
      "retryOnFail": true
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Attributes",
              "value": "={{$node[\"SplitInBatches\"].json[\"Attribute\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        90,
        720
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => { \n  return {\n    json: item,\n  }\n})\n"
      },
      "name": "Create JSON-items",
      "type": "n8n-nodes-base.function",
      "position": [
        -280,
        510
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {
          "reset": "={{$node[\"SplitInBatches\"].context[\"noItemsLeft\"]}}"
        }
      },
      "name": "SplitInBatches1",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        670,
        730
      ],
      "notesInFlow": false,
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "functionCode": "array=[]\nitem.Attributes.forEach(function(it) {\n  array.push(it)\n});\nreturn array;\n"
      },
      "name": "FunctionItem1",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        260,
        720
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => { \n  return {\n    json: item,\n  }\n})\n"
      },
      "name": "Create JSON-items1",
      "type": "n8n-nodes-base.function",
      "position": [
        510,
        450
      ],
      "typeVersion": 1,
      "alwaysOutputData": false,
      "retryOnFail": true
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"SplitInBatches1\"].context[\"noItemsLeft\"]}}",
              "value2": true
            }
          ],
          "string": []
        }
      },
      "name": "IF1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        750,
        1120
      ],
      "alwaysOutputData": false
    }
  ],
  "connections": {
    "FunctionItem": {
      "main": [
        [
          {
            "node": "Create JSON-items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "FunctionItem1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create JSON-items": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches1": {
      "main": [
        [
          {
            "node": "IF1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FunctionItem1": {
      "main": [
        [
          {
            "node": "Create JSON-items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create JSON-items1": {
      "main": [
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF1": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 8
}

Hey @mrluanpham!

Welcome to the community :sparkling_heart:

It seems like the workflow code you’ve shared is incomplete. Can you please update it?

A similar question was asked earlier. Maybe the solution I shared there can be helpful to you as well :slight_smile:

Thanks for your great tool, I updated the workflow code, please help me to review. And yes, I reviewed that topic, but some problems with my data pipeline. I think that’s Reset button problem, because my beginning data is Nested json array. Please help me

Unfortunately, there is still some issue with your workflow code. You can check the video to know how you can share the workflow code: How can I contribute? | Docs

I’ve used the workflow from the other post, and modified it a bit. Hopefully, this might be helpful,

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        260,
        290
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        750,
        280
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": true,
              "value2": "={{$node[\"SplitInBatches1\"].context[\"noItemsLeft\"]}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "position": [
        1510,
        510
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {
          "reset": "={{$node[\"SplitInBatches1\"].context[\"noItemsLeft\"]}}"
        }
      },
      "name": "SplitInBatches1",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1150,
        450
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "id",
              "value": "={{$node[\"SplitInBatches1\"].json[\"i\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set1",
      "type": "n8n-nodes-base.set",
      "position": [
        1340,
        810
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const inputData =  [\n  {id: 1, data: [{id: 10}, {id: 11}] },\n  {id: 2, data: [{id: 20}, {id: 21}] },\n];\n\nreturn inputData.map((res) => ({json: res}));"
      },
      "name": "Input data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        520,
        290
      ]
    },
    {
      "parameters": {
        "functionCode": "const res = []\nitems.forEach((item) => {\n  item.json.data.forEach((d) => {\n    res.push({...d})\n  })\n})\nreturn res.map((e) => ({json: e}));"
      },
      "name": "Flatify data with parent id",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        980,
        280
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Input data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Flatify data with parent id",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches1": {
      "main": [
        [
          {
            "node": "Set1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set1": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Input data": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatify data with parent id": {
      "main": [
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hmm, I tried some ways but my pasted code on forum cannot work when getting paste back.
Can you review my input, some problems with your workflow code when I apply it.

[
[
{
"id": "85266",
"info": "Test 1",
"Attribute": [
{
"type": "ip",
"category": "Network",
"value": "20.2.95.38"
},
{
"type": "ip",
"category": "Network",
"value": "118.69.96.3"
},
{
"type": "ip",
"category": "Network",
"value": "220.255.236.153"
}
]
},
{
"id": "87693",
"info": "Test 2",
"Attribute": [
{
"id": "5344351",
"type": "ip",
"category": "Network",
"value": "211.85.221.45"
},
{
"id": "5344354",
"type": "ip",
"category": "Network",
"value": "113.166.127.111"
}
]
},
{
"id": "87693",
"info": "Test 2",
"Attribute": [
{
"id": "5344351",
"type": "ip",
"category": "Network",
"value": "211.85.221.45"
},
{
"id": "5344354",
"type": "ip",
"category": "Network",
"value": "113.166.127.111"
}
]
}
]
];

You will have to transform the structure of the input data. Here’s the code snippet that you can refer to:

1 Like

Thanks harshil, by using Javascript code snippets, I changed my workflow to a easier one.

1 Like

I am glad you found a solution! Can you share the solution here? Other members facing the same issue can benefit from your solution :slight_smile:

Instead if using nested JSON and 2 SplitInBatches for 2 loops. I convert nested JSON into JSON array (no nested field) and 1 SplitInBatches.
My solution is that separate 2 loops instead of using nested loop for nested JSON.
Thanks

1 Like