Need help to flatten nested json array to a single array

Hi, I’ve a nested array and required to flatten before sending back to the webhook. But I’ve encounter issue as I’m not good with js. Here is the sample nested array:

[
  {
    "value": [
      {
        "UID": "FDWM_1",
        "Name": "1",
        "OBID": "6GKS01SA"
      },
      {
        "UID": "FDWM_2",
        "Name": "2",
        "OBID": "6GLE000A"
      }
    ]
  },
  {
    "value": [
      {
        "UID": "FBA_3",
        "Name": "3",
        "OBID": "6FLA001A"
      }
    ]
  },
  {
    "value": []
  }
]

and the preferred transformed output:

[
  {
    "value": [
      {
        "UID": "FDWM_1",
        "Name": "1",
        "OBID": "6GKS01SA"
      },
      {
        "UID": "FDWM_2",
        "Name": "2",
        "OBID": "6GLE000A"
      }
      {
        "UID": "FBA_3",
        "Name": "3",
        "OBID": "6FLA001A"
      }
    ]
  }
]

I attempt to use Code node to clean up the structure and the empty result but couldn’t solve it. Need to use Run Once for All Items mode. I tried to use filter and reduce but doesn’t seems to work. My workflow as below and happy if either node able to solve the problem. Thanks.

Managed to solve it using

const flattenedArray = { "value": [] };
for (const obj of $input.all()) {
  flattenedArray.value.push(...obj.json.value);
}
console.log(flattenedArray);
return flattenedArray;
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.