Merge by Key Not Working

Hello.

Can someone please tell me why this simple example of 2 functions with json data does not merge based on the matchkey key that is the same in both functions?

merge-2

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        1410,
        290
      ]
    },
    {
      "parameters": {
        "functionCode": "\nreturn[\n{\njson: {\n\"matchkey\": \"mk\",\n\"EmailAddress\": \"[email protected]\"\n}\n},\n{\njson: {\n\"matchkey\": \"dim\",\n\"EmailAddress\": \"[email protected]\"\n}\n}\n] \n"
      },
      "name": "Function3",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1600,
        170
      ]
    },
    {
      "parameters": {
        "functionCode": "return[\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"mk0410.dat\",\n      \"size\":702,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/mk0410.dat\",\n      \"matchkey\":\"mk\"\n\t   }\n   },\n   {\n\t   \t   json: {\n      \"type\":\"-\",\n      \"name\":\"mk0410.csv.txt\",\n      \"size\":740,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/mk0410.csv.txt\",\n      \"matchkey\":\"mk\"\n\t\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"dim0410.csv.txt\",\n      \"size\":740,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/dim0410.csv.txt\",\n      \"matchkey\":\"dim\"\n\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"mk0410.txt\",\n      \"size\":2431,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/mk0410.txt\",\n      \"matchkey\":\"mk\"\n\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"dim0410.dat\",\n      \"size\":702,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/dim0410.dat\",\n      \"matchkey\":\"dim\"\n\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"dim0410.txt\",\n      \"size\":2431,\n      \"modifyTime\":\"2021-10-04T13:07:11.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/dim0410.txt\",\n      \"matchkey\":\"dim\"\n\t   }\n   }\n]"
      },
      "name": "Function4",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1610,
        390
      ]
    },
    {
      "parameters": {
        "mode": "mergeByKey",
        "propertyName1": "={{$node[\"Function3\"].json[\"matchkey\"]}}",
        "propertyName2": "={{$node[\"Function4\"].json[\"matchkey\"]}}"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1830,
        260
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function3",
            "type": "main",
            "index": 0
          },
          {
            "node": "Function4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function3": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function4": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}

Am i doing something wrong?

Hey @Mulen,

Instead of using the expression builder you can just type in your JSON path based on the output of your 2 function nodes, If you set them both to matchkey it will work as expected.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        170,
        270
      ]
    },
    {
      "parameters": {
        "functionCode": "\nreturn[\n{\njson: {\n\"matchkey\": \"mk\",\n\"EmailAddress\": \"[email protected]\"\n}\n},\n{\njson: {\n\"matchkey\": \"dim\",\n\"EmailAddress\": \"[email protected]\"\n}\n}\n] \n"
      },
      "name": "Function3",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        344,
        153
      ]
    },
    {
      "parameters": {
        "functionCode": "return[\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"mk0410.dat\",\n      \"size\":702,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/mk0410.dat\",\n      \"matchkey\":\"mk\"\n\t   }\n   },\n   {\n\t   \t   json: {\n      \"type\":\"-\",\n      \"name\":\"mk0410.csv.txt\",\n      \"size\":740,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/mk0410.csv.txt\",\n      \"matchkey\":\"mk\"\n\t\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"dim0410.csv.txt\",\n      \"size\":740,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/dim0410.csv.txt\",\n      \"matchkey\":\"dim\"\n\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"mk0410.txt\",\n      \"size\":2431,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/mk0410.txt\",\n      \"matchkey\":\"mk\"\n\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"dim0410.dat\",\n      \"size\":702,\n      \"modifyTime\":\"2021-10-04T13:07:13.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/dim0410.dat\",\n      \"matchkey\":\"dim\"\n\t   }\n   },\n   {\n\t   json: {\n      \"type\":\"-\",\n      \"name\":\"dim0410.txt\",\n      \"size\":2431,\n      \"modifyTime\":\"2021-10-04T13:07:11.000Z\",\n      \"accessTime\":\"2021-10-04T13:07:20.000Z\",\n      \"rights\":{\n         \"user\":\"rw\",\n         \"group\":\"r\",\n         \"other\":\"r\"\n      },\n      \"owner\":0,\n      \"group\":0,\n      \"path\":\"/root/testemail/dim0410.txt\",\n      \"matchkey\":\"dim\"\n\t   }\n   }\n]"
      },
      "name": "Function4",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        354,
        373
      ]
    },
    {
      "parameters": {
        "mode": "mergeByKey",
        "propertyName1": "matchkey",
        "propertyName2": "matchkey"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        574,
        243
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function3",
            "type": "main",
            "index": 0
          },
          {
            "node": "Function4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function3": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function4": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}
1 Like

Thank you very much, it works as you said.

2 Likes