Repat function node based on array of json

Hi I am still struggling a bit when it comes to repeating item

I have a json which has an array of data, I want to add this data one for each item in the array after the last function node to a Postgres database I do only manage to get to add the first one

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const returnItems = [];\n\nconst mcsubscribers1 = items[0].json.mcsubscribers;\n{ \n    returnItems.push({json: {\n      id: $node[\"exmaple data\"].json[\"body\"][\"subset\"][0][\"id\"],\n      name: $node[\"exmaple data\"].json[\"body\"][\"subset\"][0][\"name\"],\n    }});\n  }\n\n\nreturn returnItems;"
      },
      "name": "Function3",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1250,
        440
      ]
    },
    {
      "parameters": {
        "functionCode": "const item= [\n{\n  json: {\n\"body\":{\n\"id\" :\"test1\",\n\"title\": \"test2\",\n\"subset\":[\n  {\n  \"id\": \"subtest1\",\n  \"name\": \"subname1\"\n},\n{\n\"id\": \"subtest2\",\n\"name\": \"subname2\"\n}\n]  \n}\n}\n}\n];\n\nreturn item;\n"
      },
      "name": "exmaple data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1020,
        440
      ]
    }
  ],
  "connections": {
    "exmaple data": {
      "main": [
        [
          {
            "node": "Function3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

There are multiple ways to write that. One in simple code would be this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const returnItems = [];\n\nfor (const item of $node[\"exmaple data\"].json[\"body\"][\"subset\"]) {\n    returnItems.push({json: item});\n}\n\nreturn returnItems;"
      },
      "name": "Function3",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const item= [\n{\n  json: {\n\"body\":{\n\"id\" :\"test1\",\n\"title\": \"test2\",\n\"subset\":[\n  {\n  \"id\": \"subtest1\",\n  \"name\": \"subname1\"\n},\n{\n\"id\": \"subtest2\",\n\"name\": \"subname2\"\n}\n]  \n}\n}\n}\n];\n\nreturn item;\n"
      },
      "name": "exmaple data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        300
      ]
    }
  ],
  "connections": {
    "exmaple data": {
      "main": [
        [
          {
            "node": "Function3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks works great. Still learning;)

Great to hear! Have fun!

Hi,

how i can have the 3 entries as an output like in each row (table)
grafik
Array to …

Thanks,
Stefan

ForEach ? Would be the right function ?

Hey @Stefan, not sure if I understand well what you want to do. I’m guessing this is the response from an HTTP request and you want to get jus the patients. If so, check the example below.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://mockup-pj5l0yxsjrbr.runkit.sh/",
        "options": {}
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        540,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\n\nfor (const item of items) {\n  for (const patien of item.json.Response.patiens) {\n    results.push({\n        json: {\n            patien, \n        }\n     })\n  }\n}\n\nreturn results\n\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        750,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

you are great !! Thanks again

1 Like