JSON newbie asking for flatmap

Hello all,

I’ve recently started using n8n and am loving it, mentioning it to people to increase its popularity :slight_smile:

I have a JSON like following and I need to flatten it to a single degree; I tried searching for it on the web and this forum, but couldn’t succeed. Any help would be appreciated…

I would like to convert this
{
“id”: “xxxxxxxxxxxxxxx”,
“userAgent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64)”,
“location”: “Istanbul, Turkey”,
“date”: “2020-12-02T11:56:05.106Z”,
“campaignId”: “yyyyyyyyyyyyyyyy”,
“customData”: {
“aaa-1111”: “Evet”,
“aaa-1112”: “Evet”
},
“data”: {
“bbb-1111”: “Evet”,
“bbb-1112”: “Evet”
},
“url”: “https://www.tttttt.com.tr/”,
“time”: 5167
}

to
{
“id”: “xxxxxxxxxxxxxxx”,
“userAgent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64)”,
“location”: “Istanbul, Turkey”,
“date”: “2020-12-02T11:56:05.106Z”,
“campaignId”: “yyyyyyyyyyyyyyyy”,
“aaa-1111”: “Evet”,
“aaa-1112”: “Evet”,
“bbb-1111”: “Evet”,
“bbb-1112”: “Evet”,
“url”: “https://www.tttttt.com.tr/”,
“time”: 5167
}

basically promoting the children of data and customdata nodes. I could prefer to get the names changed to customData.aaa-1111 and data.bbb-1111 but that’s not mandatory

Welcome to the community.

Something like the example below should do it. If customData and data properties change dynamically then a function node would be needed.

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n\"id\": \"xxxxxxxxxxxxxxx\",\n\"userAgent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\",\n\"location\": \"Istanbul, Turkey\",\n\"date\": \"2020-12-02T11:56:05.106Z\",\n\"campaignId\": \"yyyyyyyyyyyyyyyy\",\n\"customData\": {\n\"aaa-1111\": \"Evet\",\n\"aaa-1112\": \"Evet\"\n},\n\"data\": {\n\"bbb-1111\": \"Evet\",\n\"bbb-1112\": \"Evet\"\n},\n\"url\": \"https://www.tttttt.com.tr/\",\n\"time\": 5167\n}\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        520,
        300
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "bbb-1111",
              "value": "={{$node[\"Function\"].json[\"data\"][\"bbb-1111\"]}}"
            },
            {
              "name": "bbb-1112",
              "value": "={{$node[\"Function\"].json[\"data\"][\"bbb-1112\"]}}"
            },
            {
              "name": "id",
              "value": "={{$node[\"Function\"].json[\"id\"]}}"
            },
            {
              "name": "aaa-1111",
              "value": "={{$node[\"Function\"].json[\"customData\"][\"aaa-1111\"]}}"
            },
            {
              "name": "aaa-1112",
              "value": "={{$node[\"Function\"].json[\"customData\"][\"aaa-1112\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        720,
        300
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks @RicardoE105 ,

yes, I failed to mention that I don’t know what will come within data/customData, so I am after a function node. And there may be 10 items in data/customData; so manual assignment won’t work

@Bahadur check the example below

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n\"id\": \"xxxxxxxxxxxxxxx\",\n\"userAgent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64)\",\n\"location\": \"Istanbul, Turkey\",\n\"date\": \"2020-12-02T11:56:05.106Z\",\n\"campaignId\": \"yyyyyyyyyyyyyyyy\",\n\"customData\": {\n\"aaa-1111\": \"Evet\",\n\"aaa-1112\": \"Evet\"\n},\n\"data\": {\n\"bbb-1111\": \"Evet\",\n\"bbb-1112\": \"Evet\"\n},\n\"url\": \"https://www.tttttt.com.tr/\",\n\"time\": 5167\n}\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        550,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "//https://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects\nfunction flatten(data) {\n    var result = {};\n    function recurse (cur, prop) {\n        if (Object(cur) !== cur) {\n            result[prop] = cur;\n        } else if (Array.isArray(cur)) {\n             for(var i=0, l=cur.length; i<l; i++)\n                 recurse(cur[i], prop + \"[\" + i + \"]\");\n            if (l == 0)\n                result[prop] = [];\n        } else {\n            var isEmpty = true;\n            for (var p in cur) {\n                isEmpty = false;\n                recurse(cur[p], prop ? prop+\".\"+p : p);\n            }\n            if (isEmpty && prop)\n                result[prop] = {};\n        }\n    }\n    recurse(data, \"\");\n    return result;\n}\n\nconst results = [];\n\nfor (const item of items) {\n  results.push({\n    json: flatten(item.json)\n  })\n}\n\nreturn results\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

perfect! thanks!