How to combine objects into an array by their parent property?

Hey!
How to convert data from this format:

In this:

[
{
"lable": "Blue",
"value": "a",
"children": [
{
"lable": "One",
"value": "v"
},
{
"lable": "Two",
"value": "w"
}
]
},
{
"lable": "Green",
"value": "b",
"children": [
{
"lable": "Three",
"value": "x"
}
]
},
{
"lable": "Purple",
"value": "c",
"children": [
{
"lable": "Four",
"value": "y"
},
{
"lable": "Five",
"value": "z"
}
]
}
]

Hi @Roket, since you want to convert five input items into three output items, you’d need to use the Function node here. You could then write some custom JavaScript code to filter the items in the required way (keep in mind how n8n expects the data to be structured).

The function below should do it:

const results = {}

for (const item of items) {
  const data = item.json;

  if (results[`${data.name_parent}|${data.uuid_parent}`] !== undefined) {
    results[`${data.name_parent}|${data.uuid_parent}`].push({ label: data.name, value: data.uuid })
  } else {
    results[`${data.name_parent}|${data.uuid_parent}`] = [];
    results[`${data.name_parent}|${data.uuid_parent}`].push({ label: data.name, value: data.uuid })
  }
}

const response = []

for (const result of Object.keys(results)) {
  response.push(
    {
        json: {
            label: result.split('|')[0],
            value: result.split('|')[1],
            children: results[result]
        }
    }
  )
}

return response;

Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -500,
        200
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      uuid: 'v',\n      name: 'One',\n      uuid_parent: 'a',\n      name_parent: 'Blue',\n    }\n  },\n    {\n    json: {\n      uuid: 'w',\n      name: 'Two',\n      uuid_parent: 'a',\n      name_parent: 'Blue',\n    }\n  },\n      {\n    json: {\n      uuid: 'x',\n      name: 'Three',\n      uuid_parent: 'b',\n      name_parent: 'Green',\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -270,
        200
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "const results = {}\n\nfor (const item of items) {\n  const data = item.json;\n\n  if (results[`${data.name_parent}|${data.uuid_parent}`] !== undefined) {\n    results[`${data.name_parent}|${data.uuid_parent}`].push({ label: data.name, value: data.uuid })\n  } else {\n    results[`${data.name_parent}|${data.uuid_parent}`] = [];\n    results[`${data.name_parent}|${data.uuid_parent}`].push({ label: data.name, value: data.uuid })\n  }\n}\n\nconst response = []\n\nfor (const result of Object.keys(results)) {\n  response.push(\n    {\n        json: {\n            label: result.split('|')[0],\n            value: result.split('|')[1],\n            children: results[result]\n        }\n    }\n  )\n}\n\nreturn response;\n\n\n\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -70,
        200
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

@RicardoE105, thanks! You helped me a lot!