Append Merge Node

Hi,

I need help on a workflow. Here is the situation:

  • I’ve more than one text files in a git repo.
  • I need to return them as one single http page.

How should I connect the nodes and configure the workflow?

That depends how you want the file content returned. But the most basic would simply adding each of the HTTP Request nodes to one of the Merge inputs.

Here an example which will return both files in an Array:

{
  "nodes": [
    {
      "parameters": {
        "url": "https://raw.githubusercontent.com/n8n-io/n8n/master/CONTRIBUTING.md",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://raw.githubusercontent.com/n8n-io/n8n/master/README.md",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        500
      ]
    },
    {
      "parameters": {
        "path": "files",
        "responseMode": "lastNode",
        "responseData": "allEntries"
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

@jan,

It returns an array, with square brackets [ ] . Can we make it simply append two strings without brackets or anything else?

Yes, it returns it as JSON. If you do not want JSON then you have to return it as binary which is a little bit more complicated:

{
  "nodes": [
    {
      "parameters": {
        "url": "https://raw.githubusercontent.com/n8n-io/n8n/master/CONTRIBUTING.md",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://raw.githubusercontent.com/n8n-io/n8n/master/README.md",
        "responseFormat": "string",
        "options": {}
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        650,
        500
      ]
    },
    {
      "parameters": {
        "path": "files",
        "responseMode": "lastNode",
        "responseData": "firstEntryBinary"
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "const returnData = items.map(item => item.json.data);\nreturn [{json: {returnString: returnData.join('\\n\\n') }}]\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1050,
        400
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {},\n    binary: {\n      data: {\n        data: Buffer.from(items[0].json.returnString).toString('base64'),\n        mimeType: 'text/plain'\n      }\n    }\n  }\n];\n"
      },
      "name": "Make Binary",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1250,
        400
      ]
    }
  ],
  "connections": {
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Make Binary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

@jan, Thanks, you are great!

You are welcome. Great to hear that it helped. Have fun with n8n!

Hello again @jan,

I wonder if you have a solution in mind in a case where I need to merge more than 2 data nodes.

Edit 1:
Additionally, I think merging two or more outputs into one should be a very common case. Don’t you think creating a single node that does what 3 nodes does above your example. One single merge node that takes N number of inputs, append 'em and return as a single output. Just thinking from user perspective.

Edit 2:
I simply added 2 more nodes in your example, It works but I’m not sure if this is the right way.

Ah yes, agree. At some point, it would make sense to have nodes that can have n inputs and n outputs. That is however currently technically not possible and would probably require larger changes. For that reason has it not been done yet and will probably still take a while till something like that is possible.

The workaround, like you found by yourself, is to simply create multiple Merge-Nodes.
Your example would however not work like intended. You would have to remove the connection between the nodes “Merge” and “Function”. If not the nodes “Function” and “Make Binary” would get executed twice (as the first one has two connections to the same input, if you execute it you would also see a “2” on the node and all other ones have just a “1”). Once they would get executed with the merged data from nodes “Windows - Main” and “SQL - Main” and then again with the data of “Windows - Main”, “SQL - Main” and “TEST”.

1 Like