Looping via N8N (for dummies)?

Question:

I have a node that writes data to an API (using an app specific node available in n8n). This node is taking data from an array. The array often times contains 5 objects. My writing node currently references values of the object[0] and performs actions for them. This node doesn’t natively loop through the data so I’d like a way increment through the objects to change the reference to 1, 2, 3, and 4. That way my data writing node could perform the same task for items of object[0], object[1], object[2], object[3], and object[4] without having to create separate nodes for each index reference.

If the answer is use code: Can you provide an example

If you recommend the loop node, can you give a detailed example for that?

I get the data from an HTTP request (via http-get) and that works well to pull it into N8N, how do I then work with that data as mentioned above?

Thanks!!

The goal of this yet unwritten workflow (currently we read the data and have to create separate nodes to process each item):
→ Get Data from external system using http API calls → Based on the index, loop through data → for each iteration of the loop, send the data to another node

Information on your n8n setup

  • n8n version: Current
  • Database (default: SQLite): Postgresql
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Linux

The great thing about n8n is you don’t need to use the loop node, nodes run for each item by default.
Sometimes your array of items isn’t considered “items” though, so they need to be split out into items.

Please see the example below and see if that helps

After the “Split Out” node anything past that will run once with each item in the list we split out.

I hope that helps. Incase you don’t know, you can copy the embedded workflow by pressing the “this workflow’s code” above

1 Like

Thanks. Any additional help will be appreciated. How do I deal with the following?

I’m having trouble understanding how to get a specific subset of objects to be fed to the next node. The number of objects will vary so I can’t just create a separate connection for each (I am able to say to myself "there may be a max of 10 objects in the subset and then create 10 separate nodes to receive the data, but that is labor intense, creates a messy workflow, and doesn’t work if my guessed max # of objects is wrong).

To see an example of the data, see the end of this post where “all_things_list” starts (also in block-quote).

I’d like to send the array of data for each host entry (hosts.hosts[0], hosts.hosts[1], hosts.hosts[2]…) to the next node, one at a time for processing. Aka: have all_things.hosts.hosts[0] passed to the next node for processing, then all_things.hosts.hosts[1], etc etc. I want n8n to figure out how many sub objects/arrays there are (hosts[0],hosts[1]…) and treat each as a separate object (or sub-object/array) to pass on.

*** Sorry for my abuse of the terms sub-object/array, I’m not sure what the proper name is for all_things_list vs. all_things_list.data_split_out vs. all_things_list.data_split_out.hosts vs all_things_list.data_split_out.hosts.hosts[0]

Example of the data after being split:

  • all_things_list
    • data_split_out
      • hosts
        • hosts[0]
          • hostname alpha
          • IP_address 192.168.0.10
          • MAC 00:12:34:56:78:01
          • User CyberBeach
          • Comment Mac
        • hosts[1]
          • hostname beta
          • IP_address 192.168.0.11
          • MAC 00.11.22.33.44.55
          • User SomeDude
          • Comment PC
        • hosts[2]…
      • printers
        • printer[0]
          • Name LPR-1
          • IP_Address 192.168.20.10
        • printer[1]
          • Name LPR-1
          • IP_Address 192.168.20.20

You just need to use the split out node. The problem you might be running into is instead of passing an expression you pass in the key of the json where the “sub array” is, so that will need to be in the json.

You can do that with a merge node.

See these two examples, one parallel and one that is one after another. hopefully this helps

\

I hope that helps, but im not exactly sure which part you’re having trouble with. If you share your workflow and show where you’re stuck i’ll be able to help you more

Yes! I was passing the wrong info!! Thanks MUCH!! This tool is amazing, 90% I’m overthinking.

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