Creating Nested JSON from 2. JSON

Hi,

how i can add a name for the nested array after the 2. Data ( SG_Adr… )?

Thanks in advance,
Stefan

Hi @Stefan, I hope you’re having a good day thus far?

From your snippet I understand that you have multiple Items with different fields. For example your first item has the fields Suchbegriff, Strasse, Plz, Ortname, fremdnr and SG_Adressen_PK but your second item (and possibly subsequent items as well) have the fields SG_Adr_Bemerkungen, SG_Adressen_FK, SG_Sys_Bemerkty..., Bemerkung, Eintragsdatum, EingegtragenVon, and Herkunft. Do I understand that correct?

And do you want to create an array inside the first item which holds all the other items, like so?

While our Item Lists node lets you aggregate individual fields into an array, it does not let you aggregate entire objects into an array. So this would require a bit of custom JS code using the Function node, for example:

let output = items.slice(0, 1);
output[0].json.data = items.slice(1).map(item => item.json);

return output;
Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [{\n    \"json\": {\n        \"Suchbegriff\": \"foo\",\n        \"Strasse\": \"bar\",\n        \"Plz\": \"baz\",\n        \"Ortname\": \"qux\",\n        \"fremdnr\": \"quux\",\n        \"SG_Adressen_PK\": \"quuz\"\n    }    \n}, {\n    \"json\": {\n        \"SG_Adr_Bemerkungen\": \"foo\",\n        \"SG_Adressen_FK\": \"bar\",\n        \"SG_Sys_Bemerkty\": \"baz\",\n        \"Bemerkung\": \"qux\",\n        \"Eintragsdatum\": \"quux\",\n        \"EingegtragenVon\": \"quuz\",\n        \"Herkunft\": \"corge\"\n    }    \n}, {\n    \"json\": {\n        \"SG_Adr_Bemerkungen\": \"foo\",\n        \"SG_Adressen_FK\": \"bar\",\n        \"SG_Sys_Bemerkty\": \"baz\",\n        \"Bemerkung\": \"qux\",\n        \"Eintragsdatum\": \"quux\",\n        \"EingegtragenVon\": \"quuz\",\n        \"Herkunft\": \"corge\"\n    }    \n}, {\n    \"json\": {\n        \"SG_Adr_Bemerkungen\": \"foo\",\n        \"SG_Adressen_FK\": \"bar\",\n        \"SG_Sys_Bemerkty\": \"baz\",\n        \"Bemerkung\": \"qux\",\n        \"Eintragsdatum\": \"quux\",\n        \"EingegtragenVon\": \"quuz\",\n        \"Herkunft\": \"corge\"\n    }    \n}]"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "let output = items.slice(0, 1);\noutput[0].json.data = items.slice(1).map(item => item.json);\n\nreturn output;"
      },
      "name": "Transform Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Transform Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}