How to iterate over an array

I have a node that returns:

{
    "value": [
        {"id": "123"},
        {"id": "456"}
    ]
}

And I have that piped into an HTTP Request where i’m trying to call for each id, like http://blah.com/$id

What step do I need in the middle or how can I make this happen?

You simply have to make sure that each object under value ends up in an own item. You can do it for example like this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const newItems = [];\n\nfor (const item of items[0].json.value) {\n  newItems.push({json: item});\n}\n\nreturn newItems;"
      },
      "name": "Function-Transform",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "url": "=http://blah.com/{{$node[\"Function-Transform\"].json[\"id\"]}}",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json = {\n    value: [\n        {id: \"123\"},\n        {id: \"456\"}\n    ]\n};\nreturn items;"
      },
      "name": "Fake Input",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    }
  ],
  "connections": {
    "Function-Transform": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fake Input": {
      "main": [
        [
          {
            "node": "Function-Transform",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Do you know how to do the opposite? I have 9 results, but I want to combine them and make only 1 call, with pieces from each of the 9 results.

Yes, the most simple way would be like this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      a: 1\n    }\n  },\n  {\n    json: {\n      a: 2\n    }\n  },\n  {\n    json: {\n      a: 3\n    }\n  }\n];"
      },
      "name": "Mock-Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        700
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      data: items.map(data => data.json)\n    }\n  }\n]"
      },
      "name": "To-One-Item",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        700
      ]
    }
  ],
  "connections": {
    "Mock-Data": {
      "main": [
        [
          {
            "node": "To-One-Item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like