Merge by Key Example

Can you please provide an example of how to use the MERGE node, specifically with the “Merge by Key” option?

I have two sets of data below as an example. How do I setup the merge by key node to match data[].interviewers[].id from Input 1 (e.g. 111222333) with the fields.eid in Input 2 and combine the data wherever there is a match?

Thanks!

Input 1 Data:

{
“data”: [
{
“pointer”: “12345”,
“panel”: “234234”,
“subject”: “Blah Blah”,
“note”: “”,
“interviewers”: [
{
“id”: “111222333”,
“name”: “Bobby Johnson”,
“email”: "[email protected]"
}
],
“timezone”: “America/Los_Angeles”,
},
{
“pointer”: “98754”,
“panel”: “3243234”,
“subject”: “Yadda Yadda”,
“note”: “”,
“interviewers”: [
{
“id”: “444555666”,
“name”: “Billy Johnson”,
“email”: "[email protected]"
}
],
“timezone”: “America/Los_Angeles”,
},
],
“hasNext”: false
}

Input 2 Data:

[
{
“name”: “test”,
“fields”: {
“FirstName”: “Bobby”,
“LastName”: “Johnson”,
“JobTitleDescription”: “Recruiter”,
“HomeDepartmentDescription”: “Recruiting Team”,
“Photo”: [
{
“x”: “attPuc6gAIHUOHjsY”,
“url”: “http://urlto.com/BobbyPhoto.jpg”,
“filename”: “photo.jpg”,
“size”: 28956,
“type”: “image/jpeg”
}
],
“eid”: “111222333”
},
“createdTime”: “2019-09-23T04:06:48.000Z”
},
{
“name”: “test2”,
“fields”: {
“FirstName”: “Billy”,
“LastName”: “Johnson”,
“JobTitleDescription”: “CEO”,
“HomeDepartmentDescription”: “Boss Team”,
“Photo”: [
{
“x”: “attPuc6gAIHUOHjsY”,
“url”: “http://urlto.com/BillyPhoto.jpg”,
“filename”: “photo.jpg”,
“size”: 28956,
“type”: “image/jpeg”
}
],
“eid”: “444555666”
},
“createdTime”: “2019-09-23T04:06:48.000Z”
}
,
{
“name”: “test3”,
“fields”: {
“FirstName”: “Susan”,
“LastName”: “Smith”,
“JobTitleDescription”: “CFO”,
“HomeDepartmentDescription”: “Boss Team”,
“Photo”: [
{
“x”: “attPuc6gAIHUOHjsY”,
“url”: “http://urlto.com/SusanPhoto.jpg”,
“filename”: “photo.jpg”,
“size”: 28956,
“type”: “image/jpeg”
}
],
“eid”: “777888999”
},
“createdTime”: “2019-09-23T04:06:48.000Z”
}
]

Hello,

very sorry for the late answer. I started to answer this morning. Then I realized that it did not work (as dot-notation was not supported) and got then sidetracked.

Fixed it now with that commit:

I will try to release a new version with that fix later and then comment here again.

Ok is done. [email protected] is released. The new docker images should also be ready in a few minutes.

Here now the example of how you can make it work. You can simply copy the code below, click once into the n8n window and then paste it (CTRL + v). It will then create all the nodes and connections.

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json = {\n\"data\": [\n{\n\"pointer\": \"12345\",\n\"panel\": \"234234\",\n\"subject\": \"Blah Blah\",\n\"note\": \"\",\n\"interviewers\": [\n{\n\"id\": \"111222333\",\n\"name\": \"Bobby Johnson\",\n\"email\": \"[email protected]\"\n}\n],\n\"timezone\": \"America/Los_Angeles\",\n},\n{\n\"pointer\": \"98754\",\n\"panel\": \"3243234\",\n\"subject\": \"Yadda Yadda\",\n\"note\": \"\",\n\"interviewers\": [\n{\n\"id\": \"444555666\",\n\"name\": \"Billy Johnson\",\n\"email\": \"[email protected]\"\n}\n],\n\"timezone\": \"America/Los_Angeles\",\n},\n],\n\"hasNext\": false\n};\nreturn items;\n"
      },
      "name": "Data 1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        602,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "items[0].json = [\n{\n\"name\": \"test\",\n\"fields\": {\n\"FirstName\": \"Bobby\",\n\"LastName\": \"Johnson\",\n\"JobTitleDescription\": \"Recruiter\",\n\"HomeDepartmentDescription\": \"Recruiting Team\",\n\"Photo\": [\n{\n\"x\": \"attPuc6gAIHUOHjsY\",\n\"url\": \"http://urlto.com/BobbyPhoto.jpg\",\n\"filename\": \"photo.jpg\",\n\"size\": 28956,\n\"type\": \"image/jpeg\"\n}\n],\n\"eid\": \"111222333\"\n},\n\"createdTime\": \"2019-09-23T04:06:48.000Z\"\n},\n{\n\"name\": \"test2\",\n\"fields\": {\n\"FirstName\": \"Billy\",\n\"LastName\": \"Johnson\",\n\"JobTitleDescription\": \"CEO\",\n\"HomeDepartmentDescription\": \"Boss Team\",\n\"Photo\": [\n{\n\"x\": \"attPuc6gAIHUOHjsY\",\n\"url\": \"http://urlto.com/BillyPhoto.jpg\",\n\"filename\": \"photo.jpg\",\n\"size\": 28956,\n\"type\": \"image/jpeg\"\n}\n],\n\"eid\": \"444555666\"\n},\n\"createdTime\": \"2019-09-23T04:06:48.000Z\"\n}\n,\n{\n\"name\": \"test3\",\n\"fields\": {\n\"FirstName\": \"Susan\",\n\"LastName\": \"Smith\",\n\"JobTitleDescription\": \"CFO\",\n\"HomeDepartmentDescription\": \"Boss Team\",\n\"Photo\": [\n{\n\"x\": \"attPuc6gAIHUOHjsY\",\n\"url\": \"http://urlto.com/SusanPhoto.jpg\",\n\"filename\": \"photo.jpg\",\n\"size\": 28956,\n\"type\": \"image/jpeg\"\n}\n],\n\"eid\": \"777888999\"\n},\n\"createdTime\": \"2019-09-23T04:06:48.000Z\"\n}\n];\nreturn items;"
      },
      "name": "Data 2",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        602,
        550
      ]
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\n\nfor (const item of items[0].json.data) {\n  newItems.push({ json: item });\n}\n\nreturn newItems;"
      },
      "name": "Convert Data 1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        752,
        350
      ]
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\n\nfor (const item of items[0].json) {\n  newItems.push({ json: item });\n}\n\nreturn newItems;"
      },
      "name": "Convert Data 2",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        752,
        550
      ]
    },
    {
      "parameters": {
        "mode": "mergeByKey",
        "propertyName1": "interviewers[0].id",
        "propertyName2": "fields.eid"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        990,
        430
      ]
    }
  ],
  "connections": {
    "Data 1": {
      "main": [
        [
          {
            "node": "Convert Data 1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Data 2": {
      "main": [
        [
          {
            "node": "Convert Data 2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Data 1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Data 2": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}

The “Data 1” and “Data 2” nodes simply mock the data you supplied. You can replace them with the data sources. Then the “Convert Data” nodes are important. They make sure that the different array items are actually different items in n8n (want to create nodes in the future to make that simpler so that no JavaScript code has to be written for it). After that, you have then the merge with the merged data.

1 Like

Works like a charm! Thanks Jan for your fast response with an accurate working example. This is just what I needed. You’re the man!

PS - Might I suggest adding your json solution (and others) to the “workflows” section of your website? I was looking at the merge description under the nodes section of your website and at the bottom it has no associated workflows. It might help others who want to learn how to use that node if this example workflow is listed there.

Thanks again nice work I like what you’re doing!

Really great to hear that it works!

Yes you are right. I really have to add some example workflows. Would make it much easier for people. Hope I get to it soon. Try to at least add a simple version of this once I am home.

I just posted this example workflow for you. Thanks for your awesome product.

EXAMPLE WORKFLOW: Merge by Key

Cheers!

1 Like

Great, thanks a lot!