Help Needed: Dynamically Processing Variable Number of ID-Category Pairs in N8N Workflow

Hey N8N Community! :waving_hand:

I’m working on a workflow where I need to process a variable number of ID-Category pairs dynamically, but I’m running into an issue with my current approach. Would love some guidance!

Current Setup

Here’s what my workflow snippet does:

  1. Input: Receives a Product Title
  2. Internal API Call: Returns response in this format:

json

[
  {
    "output": {
      "Product_Title": "Xech Ellips Fusion of Time & sound",
      "ID1": 25625,
      "Category1": "Computer Headsets",
      "ID2": 147636,
      "Category2": "Gents Urinal"
    }
  }
]
  1. Set Node: Creates an array to feed into Split Out1 node
  2. Split Out1: Separates items to process one by one in the loop
  3. Loop Over Items: Processes each ID-Category pair

The Problem :police_car_light:

My Internal API can return up to 5 ID-Category pairs, but sometimes returns fewer (1, 2, 3, or 4 pairs).

Currently, my Set node is hardcoded to expect 5 pairs, so when the API returns fewer pairs, it creates null:null entries for the missing ones. These null pairs get sent into the loop and cause errors downstream.

Example of the issue:

  • API returns only 2 pairs (ID1/Category1, ID2/Category2)
  • Set node still creates entries for ID3-ID5, resulting in null values
  • Loop processes these null entries → Error!

What I Need

I want to handle this dynamically so that:

  • Only the actual number of returned ID-Category pairs are processed
  • No null/empty pairs are sent into the loop
  • The workflow adapts automatically whether I get 1, 2, 3, 4, or 5 pairs

Workflow Snippet

Attempted Solutions

I’ve tried modifying the Set node, but I’m not sure how to make it dynamically detect and process only the existing pairs without hardcoding.

Questions

  1. What’s the best way to dynamically create an array based on the actual number of ID-Category pairs returned?
  2. Should I use a different approach than Set → Split for this use case?
  3. Any JavaScript expressions or functions that could help filter out null/undefined pairs?

Would really appreciate any suggestions or alternative approaches! Thanks in advance! :folded_hands:

If I assume your output looks like this based on your set node:

Then your best bet is to use a code block like this in the place of your set node:

const output = $('Mock Fields 1').first().json.output;
const outputArray = Object.keys(output);
const results = [];


for (let i = 0; i < outputArray.length; i += 2) {
  const item = [output[outputArray[i]], output[outputArray[i+1]]];
  results.push(item);  
};

return {
  results
};

Which will give the results as:

Workflow:

Less output fields means a clean result:

Thanks a ton for your response.

I’ll try the same within my workflow and update here

I only notice now that your output object also contains a Product_Title field. My solution did not cater for this, so you might need to map that field out into another variable and then remove it from your object with with delete output.Product_Title;