Loop Issues on Nested Dataset

Hi there,

I am trying to loop through each sub_scene under each scene. The first scene’s sub_scenes go smoothly, but then the second loop just pass through the sub_scenes of next scenes to the done node. Please help!

{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "jsCode": "return [\n  {\n    \"Scene_count\": 0,\n    \"Scene_text\": \"blah blah blah\",\n    \"Duration\": 17.24,\n    \"Sub_scenes\": [\n      {\n        \"Scene_count\": 0,\n        \"sub_scene_count\": 1,\n        \"sub_scene_text\": \"blah blah blah\",\n        \"check\": \"No\"\n      },\n      {\n        \"Scene_count\": 0,\n        \"sub_scene_count\": 2,\n        \"sub_scene_text\": \"blah blah blah\",\n        \"check\": \"No\"\n      }\n    ]\n  },\n  {\n    \"Scene_count\": 1,\n    \"Scene_text\": \"blah blah blah\",\n    \"Duration\": 17.24,\n    \"Sub_scenes\": [\n      {\n        \"Scene_count\": 1,\n        \"sub_scene_count\": 1,\n        \"sub_scene_text\": \"blah blah blah\",\n        \"check\": \"No\"\n      },\n      {\n        \"Scene_count\": 1,\n        \"sub_scene_count\": 2,\n        \"sub_scene_text\": \"blah blah blah\",\n        \"check\": \"No\"\n      }\n    ]\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -620,
        -380
      ],
      "id": "7d76c4f4-7a2a-4311-bcfe-f07a6bcc0077",
      "name": "Scenes1",
      "executeOnce": true
    },
    {
      "parameters": {
        "jsCode": "return $json.Sub_scenes;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -260,
        -280
      ],
      "id": "b97961c3-7af0-40d4-ac20-f28ff15bae1e",
      "name": "Code"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -440,
        -380
      ],
      "id": "cd6697b1-f15e-4462-be92-dd897e52b779",
      "name": "Loop Over Items1"
    },
    {
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        100,
        -280
      ],
      "id": "185d3c55-a898-4ef9-b934-b7751226cad5",
      "name": "Loop Over Items2"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -800,
        -380
      ],
      "id": "65aae50a-af1e-4d01-b6a7-35ed439145d1",
      "name": "When clicking ‘Test workflow’"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        320,
        -180
      ],
      "id": "f404e6b6-5cc2-4f45-be0b-cf1a3f693b4a",
      "name": "No Operation, do nothing"
    },
    {
      "parameters": {
        "amount": 30
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        -80,
        -280
      ],
      "id": "608f02c2-38be-496d-9cfa-0ccc927ecde2",
      "name": "Wait",
      "webhookId": "c761e062-7c9b-46e3-837b-9664f81d3d47"
    },
    {
      "parameters": {
        "amount": 30
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        540,
        -180
      ],
      "id": "1f16ae1e-3ed0-4d8a-a0f4-8dd9106de061",
      "name": "Wait1",
      "webhookId": "8a70fdcf-6cc4-455b-928c-4e75ca57886d"
    }
  ],
  "pinData": {},
  "connections": {
    "Scenes1": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items1": {
      "main": [
        [],
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items2": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Test workflow’": {
      "main": [
        [
          {
            "node": "Scenes1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No Operation, do nothing": {
      "main": [
        [
          {
            "node": "Wait1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Loop Over Items2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait1": {
      "main": [
        [
          {
            "node": "Loop Over Items2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "558e9208-94a1-436a-93c8-4dddbf3a438b",
  "meta": {
    "instanceId": "06c5e93c401bf851c477c49cd5b904e1cb83b1c9ee3764a5af020bb35f68efe3"
  },
  "id": "weLWxDHMV3qPMBSx",
  "tags": []
}```

Hey, I don’t think you need the second loop as n8n already treat items sequentially when there are multiple.

Here’s the workflow that I think would do what you want, it gets all the sub scenes text, tell me if this helps ! :slight_smile:

Hello,

Thanks for your response. I need to have the 2nd loop as I am making HTTP API posts along with webhooks for each item. So the entire workflow is designed for 1 item at a time…

Then what is generally recommended is to do all the processing in the sub workflow that will run for each item :

You could also not use loop by using the split node :

thanks mate… is there any other solution possible to do this within the same workflow…

Hi @Asim_Siddiqui

It looks like the issue may be caused by how your loop is structured, especially with how you are passing the data between nodes.

The issue is that your second loop, "Loop Over Items2", isn’t being properly triggered by the output from the first loop. This could happen if the second loop isn’t properly connected to process the "sub_scenes" after "Loop Over Items1" finishes.

Here are some suggestions to fix the flow:

  1. Fix the connection between Loop Over Items1 and Loop Over Items2: Ensure that after processing sub_scenes in the first loop, the second loop is triggered correctly to process the next scenes.
  2. Check the output of the first loop: After Loop Over Items1, check the output of that node to make sure it is returning all of the sub_scenes as expected. You can add a “Set” node to see the data coming out and make sure it matches what you expect.
  3. Adjust the Loop Over Items nodes: The second loop should be connected to the correct output of the first loop or processed after the first loop completes. Double-check the Loop Over Items2 connections.
  4. Ensure the data is passed correctly: The first loop outputs sub_scenes for each scene, and the second loop should process those as the input. Verify if you’re passing the correct output data to the next node in the second loop.

Let me know if that helps, or if you’d like to go over the specific parts of the workflow! I hope this helps.

Thanks for your response. Can you please do a quick demo workflow, I mean fix my workflow. Thanks!

We do a couple of loops. One for scenes, another for sub-scenes.

Try if this works for you:

Hi Miquel,

Thanks for your message but it didnt work. The second loop does not process the sub-scenes of the next scene, instead pass it on towards Done node…

You are right. I changed the flow.

I extract all the scenes to generate a unique list with all of them. This way, we iterate all the scenes in a unique loop.

Check the next one:

2 Likes

Thanks Maquel. In the loop’s iteration, how do I refer to the scene this sub_scene belongs to? What I want to be able to do, is to access the current sub_scene as well as its parent scene.

You can use shortcuts in expression fields.

Just locate yourself in expression field and type {{. Autocomplete will do the job :wink:

1 Like

Thanks a lot mate. Appreciate the help!

1 Like

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