How to preserve index when using IF node

HI Everyone,
Hope you are happy and Healthy!

The problem I am facing is to preserve the Index while using SplitInBatches and If Node together,
I have prepared a scenario for the demonstration but let me give an overview first.

I am Reading data from google sheet or say from MySQL it’s huge data and I use SplitInBatches to break it down to batch of 10. Then I make an API call with HTTP node which may return 200 or any other code depending on data request i make.

After this I use IF node to check if result was 200 OK or anything else thus dividing the batch in random size(based on response of HTTP). Now I need to obtain the input value from Split in Batches which corresponds to the right index of the request so that I can mark in my databsae if API call for a row was success or Failure.

And at this point I fail as the index is no longer preserved when using IF node.

I have prepared the sample workflow to demonstrate the same.

Please check the Outcome from Set and Set1 under column “original” and compare it with the Input given to the SplitInBatches, you will notice the same index is repeated over and over for true and false both Set nodes.

Information on your n8n setup

  • n8n version: 0.163.1
  • Database you’re using (default: SQLite): MySQL
  • Running n8n with the execution process [own(default), main]: Not sure what this means
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: npm

You can try adding a set after the split with something like _pos =$position

Then when you want to reference the IF data from somewhere further down the line, use something like $item($json._pos).$node['IF'].json.

This is definitely a gotcha, and I believe people at n8n are looking into possible solutions. I’ve seen a number of people stumble over this picking up n8n, almost everyone makes the mistake once. Ideally we’d have some provenance tracking so we can say definitively that x piece of data originated from item y on node z.