General question

Hi,

I have a output which look some thing like this…

[
    {
        "finding_region": [
            {
                "component": "107285",
                "new_status": "PD"
            },
            {
                "component": "107286",
                "new_status": "PD"
            },
            {
                "component": "115532",
                "new_status": "PD"
            },
            {
                "component": "116317",
                "new_status": "PD"
            },
            {
                "component": "116322",
                "new_status": "PD"
            }
        ]
    }
]

The above is an output from a SET node…

How can i group all them togeather to getting some thing like this…

"finding_region": {
"component": "107285",
"component": "107286",
"component": "115532",
"component": "116317",
"component": "116322",
}

Basically i want to split the array and get only the component so that i can further process the values…

If there is an option to do this with out a fucntion node that would be great

Hi @Sathya_Narayanan, I hope you’re well?

The example you have provided would mean you want to transform your finding_region array into an object containing multiple keys of the same name. This will most likely get you into trouble because the JSON RFC states names within an object should be unique. You’ll most likely find that only the last key will be read by applications or programming languages for such objects, even if you manage to create this structure in n8n.

n8n itself for example is based on JavaScript. If you run the following code in your browser’s JavaScript console, you can see what I mean:

let myObject = { "finding_region": {
"component": "107285",
"component": "107286",
"component": "115532",
"component": "116317",
"component": "116322",
} }
console.log(myObject);

You will see that only component: "116322" is part of your object, everything else will be discarded.

So you might want to double-check if this is the format you want. If just having five separate items with a component field would work for you instead, this would be super easy with the Item Lists node:

image

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "fieldToSplitOut": "finding_region",
        "options": {}
      },
      "name": "Item Lists",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const example = [\n    {\n        \"finding_region\": [\n            {\n                \"component\": \"107285\",\n                \"new_status\": \"PD\"\n            },\n            {\n                \"component\": \"107286\",\n                \"new_status\": \"PD\"\n            },\n            {\n                \"component\": \"115532\",\n                \"new_status\": \"PD\"\n            },\n            {\n                \"component\": \"116317\",\n                \"new_status\": \"PD\"\n            },\n            {\n                \"component\": \"116322\",\n                \"new_status\": \"PD\"\n            }\n        ]\n    }\n];\n\nreturn example.map(e => { return { json: e } });"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "finding_region",
        "options": {}
      },
      "name": "Item Lists",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Item Lists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}