Filtering records using Split into Bunches

Hi all,

I’m new to n8n and I’m having some trouble switching my thinking from how Integromat works. I have a collection of users that I’m pulling in the first HTTP request and they all have API keys in their records. The HTTP returns the records as an array which is good, but then when I try to filter the records to see if the API Key sent via the webhook is a valid and paying user, it only compares it to the first record returned. I’ve seen the ones about Spotify and some others, but can’t seem to figure out what my workflow needs.

@pford welcome to the community.

I do not have experience with Integromat. However, by looking at the workflow I can tell that the SplitBatches node probably it’s not needed (unless you want to paginate). n8n automatically iterates over all items as long as the data follows the n8n data structure. In this case, the data coming from the HTTP request does not follow the n8n data structure. A way to map that data to something n8 understand is using a function node. Once the data is parsed correctly then the IF node should behave as expected and evaluate all items.

The example below should do what you want to do. Let me know if you have any other questions.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://mockup-pj5l0yxsjrbr.runkit.sh/",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        570,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\n\nconst users = items[0]\n\nfor (const user of users.json) {\n  results.push({ json: user})\n}\n\nreturn results;\n\n"
      },
      "name": "Map data to n8n data structure",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ],
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": true,
              "value2": "={{$node[\"Map data to n8n data structure\"].json[\"isValid\"]}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1110,
        300
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1380,
        490
      ]
    },
    {
      "parameters": {},
      "name": "NoOp1",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1410,
        190
      ],
      "notesInFlow": true,
      "notes": "Do what you want with the valid users"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Map data to n8n data structure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Map data to n8n data structure": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "NoOp1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@RicardoE105 Thank you so much for your response! I’m a longtime tinkerer in Zapier and I’m wanting to dive deeper into Javascript. N8N seems like a great way to start, but forgive me if I’m asking a dumb question as I am not a programmer or coder. Do I need to replace any part of the function in the coding block with the actual number of items or rename “users” to “records” so that it matches? Is there anything about the function node that I need to change to match my use case? See JSON below:

const results = []

const records = items[12]

for (const record of records.json) {
  results.push({ json: record})
}

return results;

As a quick update for future users. I found the section in the docs with Javascript Code Snippets to be extremely helpful in helping me get accustomed to using more code - especially if you are an Integromat or Zapier user like me.

I was able to get it working using your function with just one more level to the dot notation:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const results = [];\n\nconst users = items[0];\n\nfor (const user of users.json.records) {\n  results.push({json:user})\n}\n\nreturn results;\n\n"
      },
      "name": "Map data to n8n data structure",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        600,
        140
      ],
      "notesInFlow": true
    }
  ],
  "connections": {}
}

Ahh glad that it worked. Let me know if you any other questions.

1 Like