Merging two simple text files

Hi,

I’m new to n8n and have been struggling with merging two simple text files (no json, just plain text):

Capture

I chose Append mode for the merge. However, only the content of Input 2 appears in the merged output.

Please let me know if this is possible and what I have done wrong.

Thanks in advance.

I don’t know, For some weird reasons, I can’t access the merged data from the Expressions Tab.
Not sure this is a bug @harshil1712

image

But you can use the “Copy Item Path” to copy the expression


I have created one workflow similar to your usecase.

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "TextfromTWO",
              "value": "Example word is"
            }
          ]
        },
        "options": {}
      },
      "name": "Document 2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        630,
        400
      ]
    },
    {
      "parameters": {},
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        850,
        320
      ]
    },
    {
      "parameters": {
        "fileName": "/data/example.file",
        "dataPropertyName": "={{ $item(\"1\").$node[\"Merge\"].json[\"TextfromTWO\"] }}\n{{$node[\"Merge\"].json[\"0\"][\"word\"]}}"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1050,
        320
      ]
    },
    {
      "parameters": {
        "url": "https://random-words-api.vercel.app/word",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        630,
        230
      ]
    }
  ],
  "connections": {
    "Document 2": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Welcome to the community @appml

To do that, you need to use a function node. Check the example below.

const binaryData1 = Buffer.from(items[0].binary.data.data, 'base64').toString()

const binaryData2 = Buffer.from(items[0].binary.data1.data, 'base64').toString()

return [
  {
    json: {},
    binary: {
      data: {
        ...items[0].binary.data,
        data: Buffer.from(binaryData1 + '\n' + binaryData2).toString('base64')
      }
    }
  }
]
Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        60,
        510
      ]
    },
    {
      "parameters": {
        "mode": "mergeByIndex"
      },
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        880,
        500
      ]
    },
    {
      "parameters": {
        "url": "https://filesamples.com/samples/document/txt/sample3.txt",
        "responseFormat": "file",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        510,
        420
      ]
    },
    {
      "parameters": {
        "url": "https://filesamples.com/samples/document/txt/sample1.txt",
        "responseFormat": "file",
        "dataPropertyName": "data1",
        "options": {}
      },
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        510,
        650
      ]
    },
    {
      "parameters": {
        "functionCode": "const binaryData1 = Buffer.from(items[0].binary.data.data, 'base64').toString()\n\nconst binaryData2 = Buffer.from(items[0].binary.data1.data, 'base64').toString()\n\n\nreturn [\n  {\n    json: {},\n    binary: {\n      data: {\n        ...items[0].binary.data,\n        data: Buffer.from(binaryData1 + '\\n' + binaryData2).toString('base64')\n      }\n    }\n  }\n]\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1140,
        500
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}
1 Like

Thanks a lot. However I am running into another error:

Thank you @RicardoE105. Finally I got it to work with a small modification of your function.

:+1:

1 Like