How to loop through array of URLS

I have a FUNCTION node that produces an array with string URL’s. I want to pass this to an HTTP node to call each of these URLS and then merge the data. Any ideas?

Hey @go4cas!

Do you have multiple items, or do you have a single item (an array in your case), that contains all the URLs? If you have a single item, you will have to convert it into multiple items. Here’s the code snippet that will be helpful: JavaScript Code Snippets | Docs

Thanks, @harshil1712 … I have tried both these inputs:

    [
    [
    {
    "file":"https://raw.githubusercontent.com/CSSEGISandData/COVID19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-01-2021.csv"
    },
    {
    "file":"https://raw.githubusercontent.com/CSSEGISandData/COVID19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-02-2021.csv"
    },
    {
    "file":"https://raw.githubusercontent.com/CSSEGISandData/COVID19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-03-2021.csv"
    }
    ]
    ]

and
    [
    [
    "https://raw.githubusercontent.com/CSSEGISandData/COVID19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-01-2021.csv",
    "https://raw.githubusercontent.com/CSSEGISandData/COVID19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-02-2021.csv",
    "https://raw.githubusercontent.com/CSSEGISandData/COVID19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-03-2021.csv"
    ]
    ]

But, I’m not sure how to get each URL in the HTTP node?

For n8n, this is a single item and not multiple items. You need to have a structure as below to have multiple items that n8n can handle for you

[
  {
    "URL":"URL-1"
  },
  {
    "URL":"URL-2"
  },
  {
    "URL":"URL-3"
  },
]

To generate this structure, your code snippet should be as follow:

return [
  {
    json: {
      "url":'url-1'
    }
  },
  {
    json: {
      "url":'url-1'
    }
  },
  {
    json: {
      "url":'url-1'
    }
  }
]

That worked! Thanks, @harshil1712!

1 Like