Loop and then return the loop results

Hi
Merry Christmas to everyone in the community.

I have a JSON payload like this.

  1. I need to create rows from all the “Rows” elements with the names of the columns from these elements (please help).
  2. Send this whole table to Google table (I know that).
  3. Based on the created table, send a separate request for each row to another service (how to loop or something like that).
  4. So iterate over all the lines and believe the answer to the entry point from the first JSON (please help).

I think this should work out for you:

  "nodes": [
    {
      "parameters": {
        "path": "",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        450,
        220
      ],
      "webhookId": ""
    },
    {
      "parameters": {
        "functionCode": "const data = items[0].json.Rows;\nconst result = data.map(i => {\n  return ({json:i});\n});\nreturn result;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        630,
        220
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "name": "SplitInBatches",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        820,
        220
      ]
    },
    {
      "parameters": {},
      "name": "Your External service",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1010,
        220
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$node[\"SplitInBatches\"].context[\"noItemsLeft\"]}}",
              "value2": true
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1210,
        410
      ]
    },
    {
      "parameters": {},
      "name": "END",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1470,
        390
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches": {
      "main": [
        [
          {
            "node": "Your External service",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Your External service": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "END",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SplitInBatches",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}  ```

Thanks, also Merry Christmas!

All you have to do is to convert the data underneath “Rows” into separate items. So with a Function-Node with the following code:

// Based on:
// https://docs.n8n.io/reference/javascript-code-snippets.html#_1-create-multiple-items-from-a-single-item
return items[0].json.Rows.map(item => {
  return {
    json: item
  }
});

After you did that everything else will be easy. You can simply follow the documentation for the Google Sheets node to append the data and also the one of the HTTP Request Node. n8n will iterate automatically over all the items.

Thanks Jan
Yes, it works, just added the “body” correction to the code.

1 Like

You are welcome!

Ah yes makes sense! Did see it wrong in the picture.

Have fun!

1 Like