JSON Parsing URL Attribute

Hey team,

I have only really started picking up N8N in an effort to simplify some of my IFTTT and other aggregate services, so please excuse me if I may have missed something, and do feel free to point me at a workflow or tutorial so I can work through what I am doing.

Initial PoC I am keen to get working:

Get JSON data via HTTP request > Parse output and another HTTP Request to loc attribute (URL) > Parse data to extract headings > Webhook to Discord

In essence, I have a JSON feed of news websites I want to keep across, and I’d like N8N to be able to perform a HTTP request to each. I have no issues receiving my JSON object into N8N, I think I just need a function to only grab the loc attribute from all items - though, I don’t know if I need to build a new object/array of just those URLs, or can I pass them straight into a HTML Request somehow. I don’t plan on using the other JSON data in this first instance.

I’ve provided a dummy below to show current structure.

{
  "sites": [
    {
      "loc": "https://www.newsite1.com/",
      "lastmod": "2020-12-19",
      "changefreq": "daily",
      "priority": "0.7"
    },
    {
      "loc": "https://www.newsite2.com/",
      "lastmod": "2020-12-20",
      "changefreq": "daily",
      "priority": "0.8"
    },
    {
      "loc": "https://www.newsite3.com/",
      "lastmod": "2020-12-18",
      "changefreq": "daily",
      "priority": "0.4"
    }
  ]
}

Am I approaching this the right way with N8N? or is there another way I should be handling the JSON objects rather than within a function (ie. maybe the HTML Extract is good at this natively?)

Welcome to the community @burnman!

What you have to do is to split each of the sites in an own item. As soon as you have that you can referene the “loc” property directly in the HTTP Request node and it will do a request for each page.

Here a link to the documentation which describes how that is possible:

And here an example workflow which implements it with the example-data you did provide:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n  \"sites\": [\n    {\n      \"loc\": \"https://www.newsite1.com/\",\n      \"lastmod\": \"2020-12-19\",\n      \"changefreq\": \"daily\",\n      \"priority\": \"0.7\"\n    },\n    {\n      \"loc\": \"https://www.newsite2.com/\",\n      \"lastmod\": \"2020-12-20\",\n      \"changefreq\": \"daily\",\n      \"priority\": \"0.8\"\n    },\n    {\n      \"loc\": \"https://www.newsite3.com/\",\n      \"lastmod\": \"2020-12-18\",\n      \"changefreq\": \"daily\",\n      \"priority\": \"0.4\"\n    }\n  ]\n}\n  }\n];"
      },
      "name": "Mock-Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{$json[\"loc\"]}}",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.sites.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Split Into Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Mock-Data": {
      "main": [
        [
          {
            "node": "Split Into Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Into Items": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like