Loop through numbered array

Describe the problem/error/question

I’m not sure how to Loop through an array with randomly names numbered keys. This is the output of my node. How do I run a set that has each array object in a table.

[
  {
    "1": {
      "id": "1",
      "url": "https://ex1.com/",
      "name": "Ex1"
    },
    "4": {
      "id": "4",
      "url": "https://ex2.co/",
      "name": "Ex2"
    },
    "7": {
      "id": "7",
      "url": "https://ex3.com/",
      "name": "Ex3"
    },
   ...
]

Information on your n8n setup

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

Hi @jyoansah, welcome to the community :tada:

The example data you have shared looks like you have an object (with keys such as 1, 4, 7, etc. rather than an array). So in order to run an action against each of the objects under these keys you’d have to convert it into an array first.

Here’s a quick example workflow doing this using a bit of code based on the example data you have provided:

After the “Convert to array” Code node you should be able to use your n8n nodes as expected (as you now have multiple n8n items rather than a single object) and can loop through all of them:

Hope this helps! Let me know if you have any questions on this.

1 Like

This is great! Thank you so much.

1 Like

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