How to iterate via the GUI

Good afternoon all,

I have previously been using Integromat for my automation need but wanted to try n8n as one or two scenarios are just too costly via integromate.

I am struggling to iterate over the output of a API query.

So this scenario queries an API to get data

Then use SET node to get the data where I need it

and push it to a google sheets to append the row, and this is fine but only does one result.

When you look at the originally API request there are over 600+ results under objects
image

each having similar data for each result

Is there a way I can use the GUI to tell it to iterate over each one of these in the array?
In Integromat there is a way to do this in the GUI
image

If there is no way to do it via the GUI, what is the easiest way to take this data from the previous node and iterate over it to then export it to google sheets? I’m not strong on javascript.

Thanks in advance.

Hey @RedPacketSec!

You will have to combine all these results into one single item. You will have to use the Function node to do it. You can refer to the code snippet mentioned in the documentation. I would also suggest you to take a look at the Data Structure documentation to understand the structure n8n uses.

Hope this helps :slight_smile:

i will have a look and test, but any plans for a GUI iterator :wink: ?

maybe i am missing something, but using that same documented workflow i still get one output its just nested it all in data_objects, but there are still multiple arrays below in there to process exactly as before

Hey @RedPacketSec, is it possible to share the URL of the API? That would be helpful to prescribe the solution using the Function node. Feel free to DM me, in case you cannot share it publicly :slight_smile:

No i am not able to share the URL, but let me see if i can find another public service that has nested API arrays…

1 Like

Try https://api.coinpaprika.com/v1/tickers

   #This is the format of the API output of MY API service
 {
    "meta": {
        "limit": 1000,
        "next": "/api/v1/xxxxxxxxxxxxx",
        "offset": 0,
        "previous": null,
        "total_count": 75679
    },
    "objects": [
        {
            "a": "1",
            "b": "2",
            "v": "3",
         }
        {
            "a": "1",
            "b": "2",
            "v": "3",
         }
    ]
}

I need each nested objects to be processed and end up here

to then end up here

Do you want to iterate just over the first page (the first 1000 items according to the limit)? Or do you want o iterate over all pages?

I will be limiting it to the first page in this instance.

Hey @RedPacketSec, perhaps this helps?

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.objects.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json.objects = [\n        {\n            \"a\": \"1\",\n            \"b\": \"2\",\n            \"v\": \"3\",\n         },\n        {\n            \"a\": \"1\",\n            \"b\": \"2\",\n            \"v\": \"3\",\n         }\n];\nreturn items;"
      },
      "name": "Mock data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Mock data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock data": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I used the code snippet from the URL that @harshil1712 shared. Hope that helps!

yes, this is what i tried before but must have did it wrong, tired brain problem after trying for too long!
return items[0].json.objects.map(item => {
  return {
    json: item
  }
});

But this works now, thank you so much for helping me, you must get these simple problems all the time. Sorry for wasting your time due to my user error it seems!

1 Like

No worries at all, I am happy to help! Glad to hear that it works now. Have fun! :raised_hands: