Can someone please explain loops to me

Hiyas

I have been using N8N for some time, self hosted and I have several workflows setup and working.

I hope this isn’t too dumb of a question but can someone help me understand loops. Specifically, I have a “order” data structure, with 1 or more line items. Pretty standard.

I need to do processing for each line item. In my case, for each line item I need to look at the product ID of the line item and then do some processing. In this case the “processing node” is a switch statement.

I tried referencing the product ID in the routing rules of the switch statement but it references an individual array item and only processes that one

{{ $(‘WooCommerce Trigger’).item.json[“line_items”][0][“product_id”] }}

I am sorry if I am not being descriptive enough, if you would be kind enough to ask me some questions I can answer.

I have experience with integration platforms all the way back to webmethods, I just cant get n8n loops to click in my head.

thank you for your help

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

When a node returns a list of items, n8n automatically loops through them. Thus, in most cases, you do not need to use the loop node.

In the node following the one that returns a list of items, you can directly access the item information, for example, {{ $json.product_id }}.

It is “slightly counterintuitive,” but ultimately, it makes the workflows much more readable, clean, and quick to develop.

I hope this was clear and helpful!

The loop node may be helpful if you will need to process one item with different nodes, as it behaves differently.

In the usual way, the items are being processed in such order:

  • node 1 >> item 1
  • node 1 >> item 2
  • node 1 >> item 3
  • node 2 >> item 1
  • node 2 >> item 2
  • node 2 >> item 3

In the loop node they will be processed in different orders:

  • node 1 >> item 1
  • node 2 >> item 1
  • node 1 >> item 2
  • node 2 >> item 2
  • node 1 >> item 3
  • node 2 >> item 3

here is an example of how it works

In your example, I see there are 3 items coming from the first step. So the loop steps runs 3 times.

In my data structure, I have 1 order and inside of that are 3 line items. So the next step only seem to see 1 item because it looks at the outer most data structure. How do I point it to the inside line items array?

Ok I think I got it.

I used a set node to pull out the line items from 3 nodes previous. The line items were still nested 3 layers deep as in the original data structure.

Then I used split out on that output to make the items at the top level. Then I get multiple items coming out and the next node runs once per item.

Does this sound like the way you are supposed to do it? I couldnt find a way to reference the original nested list from 3 nodes back in split out, so I had to use the set node before it

1 Like

Yes. If you provide a workflow with an example, It will be more useful.

In my example, there are 3 items, and each of them has a nested 3 items (so it is 9 in total).

I really like and appreciate N8N, I use it every few days in my work.

I say with love and respect that I think N8N is doing a poor job of explaining loops in the documentation. The same few examples and tag lines are usually quoted. The loop system is no doubt elegant but it has been very hard to understand how they work.

I wouldnt say I understand but I have fumbled through enough to make my code work.

2 Likes

Hi @louiszezeran,
Thanks for your feedback regarding the documentation on loops. I’ve created an internal ticket for the docs team to take a look!

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