Using Merge node

Hi, Is it possible to get output on merge node, if data is not available on either one of those inputs.
Something like bypassing the data, based on availability on input. To return data from whichever input has data available on it.

You can use “append”. If only one has data it will only return the data of that one. If none has data then nothing.

1 Like

I have been trying to test an API, but merge node is not getting executed. I Thought its an issue with the inputs.
Here’s the workflow:

{
  "name": "petstore_api",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        80,
        380
      ]
    },
    {
      "parameters": {
        "requestMethod": "POST",
        "url": "https://petstore.swagger.io/v2/pet",
        "jsonParameters": true,
        "options": {
          "fullResponse": true,
          "ignoreResponseCode": true
        },
        "bodyParametersJson": "{\t\"id\": 10,\t\"category\": {\t\t\"id\": 44,\t\t\"name\": \"Tommy\"\t},\t\"name\": \"doggie\",\t\"photoUrls\": [\t\t\"https://jsonlint.com/\"\t],\t\"tags\": [{\t\t\"id\": 23,\t\t\"name\": \"Tommy\"\t}]}"
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        860,
        380
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Spreadsheet File1",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        420,
        380
      ]
    },
    {
      "parameters": {
        "filePath": "C:\\Users\\Kshitij.bharde\\Downloads\\PetResult.xlsx"
      },
      "name": "Read Binary File",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        1030,
        -30
      ]
    },
    {
      "parameters": {},
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 1,
      "position": [
        1470,
        190
      ]
    },
    {
      "parameters": {
        "fileName": "C:\\Users\\Kshitij.bharde\\Downloads\\PetResult.xlsx"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1770,
        190
      ]
    },
    {
      "parameters": {
        "operation": "toFile",
        "options": {}
      },
      "name": "Spreadsheet File3",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        1620,
        190
      ]
    },
    {
      "parameters": {
        "fileSelector": "C:\\Users\\Kshitij.bharde\\Downloads\\PetData.xlsx"
      },
      "name": "Read Binary Files1",
      "type": "n8n-nodes-base.readBinaryFiles",
      "typeVersion": 1,
      "position": [
        220,
        380
      ]
    },
    {
      "parameters": {
        "batchSize": 1
      },
      "name": "SplitInBatches1",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 1,
      "position": [
        650,
        380
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "name": "Spreadsheet File2",
      "type": "n8n-nodes-base.spreadsheetFile",
      "typeVersion": 1,
      "position": [
        1240,
        -30
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$node[\"HTTP Request\"].json[\"statusCode\"]}}",
              "operation": "equal",
              "value2": "={{$node[\"SplitInBatches1\"].json[\"ResponseCode\"]}}"
            }
          ]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1000,
        220
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "API",
              "value": "={{$node[\"SplitInBatches1\"].json[\"API\"]}}"
            },
            {
              "name": "STATUS_CODE",
              "value": "={{$node[\"HTTP Request\"].json[\"statusCode\"]}}"
            },
            {
              "name": "MESSAGE",
              "value": "={{$node[\"HTTP Request\"].json[\"statusMessage\"]}}"
            },
            {
              "name": "RESULT",
              "value": "PASS"
            }
          ]
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1230,
        140
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "API",
              "value": "={{$node[\"SplitInBatches1\"].json[\"API\"]}}"
            },
            {
              "name": "STATUS_CODE",
              "value": "={{$node[\"HTTP Request\"].json[\"statusCode\"]}}"
            },
            {
              "name": "MESSAGE",
              "value": "={{$node[\"HTTP Request\"].json[\"statusMessage\"]}}"
            },
            {
              "name": "RESULT",
              "value": "FAIL"
            }
          ]
        }
      },
      "name": "Set2",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1230,
        280
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Read Binary Files1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet File1": {
      "main": [
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Binary File": {
      "main": [
        [
          {
            "node": "Spreadsheet File2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Spreadsheet File3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet File3": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Binary Files1": {
      "main": [
        [
          {
            "node": "Spreadsheet File1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SplitInBatches1": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spreadsheet File2": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Set2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          },
          {
            "node": "Read Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set2": {
      "main": [
        [
          {
            "node": "Read Binary File",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Write Binary File": {
      "main": [
        [
          {
            "node": "SplitInBatches1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": "42"
}

Can you tell if there’s anything wrong that I am doing.

The workflow gets stopped when there’s a false output. I want this loop to keep running for all the values in the spreadsheet(which is 5). It hits an error at 3rd value(doesn’t match with the expected data) after that execution stops at merge node.

For the merge node to work it needs data on both inputs. In your example, you can see that the node “Merge” gets executed only 2x because it only receives data for 2 iterations from both inputs. Then on the 3rd one, it receives data via input 1 but none for input 2 (as the data there has only data for iteration 1 & 2 on Node “Set” and iteration 1 for “Set 2”. So both nodes that go into “Merge” need the green number in the corner to say “3” that it works.

So it should probably work if you simply add a node in between like a “NoOp” node and then connect this node to input 2 of “Merge”.

In general, you should never connect more than one node to input 2 of a merge node else it will not work correctly.

I hope that helps.

1 Like

@Kshitij_Bharde does it work now or are you still having problems?

The execution is continuing now, but whenever a condition fails in IF node then the data that I get on the set2 is of previous execution, is there any workaround to get the proper data for that iteration?

Changed the expression behavior in [email protected]

Please update to that version and then try again.

1 Like

Thanks Jan it’s working now :slight_smile:

1 Like

Great to hear! Have fun!