Converting Nested array to string using keys

Hello there, is there a simple way to convert a nested array using a function or Set node? I’m trying to transform json like this:

[{
	"allData": [{
			"counter": 2,
			"name": "Sample String 1"
		},
		{
			"counter": 2,
			"name": "Sample String 2"
		}
	],
	"update": {
		"count": 2
	}
}]

to this:

Sample String 1, Sample String 2

The example below should do it:

  {
      "nodes": [
        {
          "parameters": {},
          "name": "Start",
          "type": "n8n-nodes-base.start",
          "typeVersion": 1,
          "position": [
            250,
            300
          ]
        },
        {
          "parameters": {
            "functionCode": "return [\n  {\n    json: [{\n\t\"allData\": [{\n\t\t\t\"counter\": 2,\n\t\t\t\"name\": \"Sample String 1\"\n\t\t},\n\t\t{\n\t\t\t\"counter\": 2,\n\t\t\t\"name\": \"Sample String 2\"\n\t\t}\n\t],\n\t\"update\": {\n\t\t\"count\": 2\n\t}\n}]\n  }\n]\n"
          },
          "name": "Function",
          "type": "n8n-nodes-base.function",
          "typeVersion": 1,
          "position": [
            540,
            300
          ],
          "notesInFlow": true,
          "notes": "Mockup data"
        },
        {
          "parameters": {
            "functionCode": "\nreturn [\n  {\n    json: {\n      names: items[0].json[0].allData.map((e) => e.name).join(',')\n    },\n  }\n]"
          },
          "name": "Function1",
          "type": "n8n-nodes-base.function",
          "typeVersion": 1,
          "position": [
            750,
            300
          ],
          "notesInFlow": true,
          "notes": "Map data"
        }
      ],
      "connections": {
        "Start": {
          "main": [
            [
              {
                "node": "Function",
                "type": "main",
                "index": 0
              }
            ]
          ]
        },
        "Function": {
          "main": [
            [
              {
                "node": "Function1",
                "type": "main",
                "index": 0
              }
            ]
          ]
        }
      }
    }

I’m getting an error when attempting to apply the example to my final project, but in the example I was able to replicate the reason for the error. The actual format of my data is:

return [
  {
    json: {
	"allData": [{
			"counter": 2,
			"result": "Sample String 1"
		},
		{
			"counter": 2,
			"result": "Sample String 2"
		}
	]
}
  }
]

And this is the example showing the error with the data being output in way my project outputs it:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        150,
        550
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n\t\"allData\": [{\n\t\t\t\"counter\": 2,\n\t\t\t\"result\": \"Sample String 1\"\n\t\t},\n\t\t{\n\t\t\t\"counter\": 2,\n\t\t\t\"result\": \"Sample String 2\"\n\t\t}\n\t]\n}\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        340,
        550
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "\nreturn [\n  {\n    json: {\n      names: items[0].json[0].allData.map((e) => e.result).join(',')\n    },\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        550
      ],
      "notesInFlow": true,
      "notes": "Map data"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

ahh yes, the example that I sent uses an array instead of an object. Anyways, the function node should look like this:

return [
  {
    json: {
      names: items[0].json.allData.map((e) => e.name).join(',')
    },
  }
]
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -130,
        190
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n\t\"allData\": [{\n\t\t\t\"counter\": 2,\n\t\t\t\"name\": \"Sample String 1\"\n\t\t},\n\t\t{\n\t\t\t\"counter\": 2,\n\t\t\t\"name\": \"Sample String 2\"\n\t\t}\n\t],\n\t\"update\": {\n\t\t\"count\": 2\n\t}\n}\n  }\n]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        50,
        190
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "\nreturn [\n  {\n    json: {\n      names: items[0].json.allData.map((e) => e.name).join(',')\n    },\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        260,
        190
      ],
      "notesInFlow": true,
      "notes": "Map data"
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

It works!!! Thank you so much! Do you happen to know any good resources for learning the javascript needed to write these functions myself? Regardless, thanks again!

1 Like

Glad that it worked. The resources below should help you with that.

https://docs.n8n.io/reference/function-nodes.html#function-and-function-item-nodes

2 Likes