Iterating over multiple arrays in a flow

Hi all,

I am looking to create a workflow which does multiple HTTP requests to a service. Each HTTP returns N amount of items within an array, and the subsequent modules should iterate over each array of the modules before. (Hope im being clear).
This would for example mean:

HTTP1: gets array of 2 items
HTTP2: iterates 2x from HTTP1 , gets 2 items per item; finally 4 items
HTTP3: iterates 4x from data before (HTTP1 and HTTP2), gets 2 per item; finally 8 items
etc.

Now it looks like my flow either doesn’t work like I expect it to work, and I am missing something, or there is a bug. I am using the formatters between HTTP modules to make sure the array is created according to the n8n standards.

As you can see in the screenshots, the “GetAxis” HTTP module iterates correctly the first time, but is missing critical values from earlier modules from iteration 2 forward (The “Token” and “machinecode” variables). How should I tackle this?


@Bjorn welcome to the community

Do not think you need the functions nodes to map the data to some n8n understands. You can do the same thing with an Item List node and split out items operation.

Try doing {{$item(0).$node["Format token json"].json["token"]}} instead of {{$node["Format token json"].json["token"]}}

Hey @RicardoE105 , thank you for the reply.

In the Token that would not be possible, since it checks the data with an if statement and only continues if the data is correct and then returns the token.
On the other HTTP requests this could work, but still I am finding it weird that later within the workflow it doesn’t grab the earlier variables anymore except for the first item in the array (see screenshots above), can you explain.

Looks like now I am doing something wrong with the expression you gave:

Using this var:

{{$item(0).$node["EiAnalytics - GetCompanies"].json["0"]["Value"]}}

This is the data coming in:


[
[
{
"Key": {
"Configs": [
],
"Description": "OK",
"StatusNumber": 0
},
"Value": [
{
"Description": "",
"Id": 3,
"Name": "Titan America PNS",
"Type": "Company"
}
]
}
]
] 

@RicardoE105 I see now the split module requires plain text…which in this case is not possible.

I see now what you mean with the {{$item(0).$node["Format token json"].json["token"]}} expression instead of the other one.
However, even if this would work with the token (since there is only 1 token), this would not work with the “Machines / machinecode” since there could be multiple machines. If lets say we have 2 machines we want to go over the HTTP requests coming after this module for each machine. So using item(0) would only grab the first machine?

Please let me know thanks!

Ok, this is difficult to debug without looking at the workflow. Can you create a loom video explaining the issues or maybe a workflow with mockup data that I can use to better understand the problem?

@RicardoE105 So I saw this post (Google Sheets create New Sheet with Every Item) regarding something similar, and now I understand your earlier reply about the item index.

This can be used for the token, since there is only 1 within the array, but what about the modules that should be running multiple times for each item in an array?
Lets say we have
token array: [ “dfgufng”]
machine array: [“machine 1”, “machine 2”]
points array, for each machine: [1, 2, 3]

I want to run an HTTP request to get the axis of each point, which is related to each machine.
So for each machine I want to get the points, which then results in an array of 3, and for each point of that machine I want to run an HTTP request to get the axis of this point.

It looks like N8N doesn’t handle this kind of looping like I expected…

n8n iterates for as many inputs as the node has. The last node in your example receives 3 items but you are referencing a node (using expressions) that outputs 8 items. That is the problem since n8n uses the iteration index to resolve the expressions. To do what you want to do, you need to create the input of the last HTTP request either using a merge node with the multiplex operation or with a function node. The example below uses the merge node with the multiplex operation.

So for everyone who is also interested, and for @RicardoE105 to check if this is what you mean, I eventually created this flow with merges to make sure the arrays got iterated over each other.

@RicardoE105 How does that look to you ? :slight_smile: