n8n tutorial - Lesson 03: Branching Workflows: IF, Switch & Merge Nodes in n8n
Hi everyone, in this post we are diving into one of the most useful parts of any n8n workflow automation setup — branching logic. You will learn how to use the IF node, the Switch node, and the Merge node in n8n to split, route, and recombine your data like a pro. This is part of our ongoing n8n Workflow Automation Tutorial series, so if you are new here, check out the earlier posts before jumping in.
What Are Branching Nodes in n8n?
When you build real-world automations, not every item should follow the same path. Some tasks are done, some are pending. Some emails are spam, some are not. Branching nodes let you check a condition and send each item down the right path. In this n8n tutorial, we will work through three branching tools: IF, Switch, and Merge, using real workflows from our learning sessions.
How to do:
Part 1 — Using the IF Node to Split Items into Two Paths
-
Step 1: Open your existing workflow. In our session we used the workflow named T1-B1-Hello-n8n, which already had an Edit Fields node pulling in todo items from an API.
-
Step 2: Click the + button after the Edit Fields node and search for IF. Add the IF node to your canvas.
-
Step 3: Inside the IF node, set up your condition. Click Add Condition, choose the field completed, set the operator to is true, and use the expression {{ $json.completed }}. This tells n8n: "if the completed field is true, go left; otherwise go right."
-
Step 4: Run the workflow. With 10 items in our test, the IF node split them into 3 true (completed tasks) and 7 false (incomplete tasks). You will see two output branches appear on the node — true on the left and false on the right.
-
Step 5: Add a Set node to the true branch. Rename it Set Done and add a field called status with the value Done.
-
Step 6: Add another Set node to the false branch. Rename it Set Pending and add a field called status with the value Pending.
Quick tip from our session: You can also use the IF node with Gmail data. For example, set a condition like labelIds contains SPAM to route spam emails into a separate processing path. However, if your data source already supports filtering (like using query parameters in an HTTP Request), it is better to filter at the source rather than using an IF node — this keeps your workflow cleaner and faster.
Part 2 — Using the Switch Node to Route Items into Multiple Paths
-
Step 1: Create a new workflow and name it T1-B4-Switch-Demo. Add an HTTP Request node, rename it Get 60 Todos, and point it to the JSONPlaceholder todos API. Add a query parameter _limit with value 60. This returns 60 todo items spread across three user IDs: 1, 2, and 3.
-
Step 2: Click the + button after the HTTP Request node and search for Switch. Add the Switch node and rename it Route by userId.
-
Step 3: Inside the Switch node, add three rules. For each rule, set the data type to Number, the field to {{ $json.userId }}, the operator to equal, and the values to 1, 2, and 3 respectively. This creates three output branches — one for each user group.
-
Step 4: Run the workflow. Each branch should receive exactly 20 items (60 items divided by 3 user IDs). The Switch node routes each item to the matching output based on its userId value.
-
Step 5: Try the Fallback behavior. Change the _limit to 100 and re-run. Now 40 extra items with userId 4 and 5 do not match any rule, so they fall into the Fallback output. This is very useful in production — always plan for unexpected values by handling the Fallback branch.
-
Step 6: Add a Set node after each branch. Name them Label Marketing, Label Engineering, and Label Sales. In each node, add a field called team with the corresponding team name (Marketing, Engineering, Sales). Make sure to set Include Other Input Fields to All — we will explain why this matters in the Code node section below.
Switch vs IF: Use the IF node when you have two paths (true/false). Use the Switch node when you have three or more possible routes. Think of Switch as a smarter, more scalable version of IF for multi-branch scenarios. Also remember that Switch rule indexes are 0-based — output 0 is the first rule, output 1 is the second, and so on.
Part 3 — Using the Merge Node to Combine All Branches
-
Step 1: Still inside T1-B4-Switch-Demo, click the + button and search for Merge. Add it to the canvas and rename it Combine All Teams.
-
Step 2: Inside the Merge node settings, set the Mode to Append and set Number of Inputs to 3. This tells n8n to accept data from three separate branches and join them into one stream.
-
Step 3: Connect the output of Label Marketing to Input 1 of the Merge node. Connect Label Engineering to Input 2 and Label Sales to Input 3.
-
Step 4: Run the workflow. The Merge node output should show all 60 items — 20 from Marketing, then 20 from Engineering, then 20 from Sales, joined one after another. In Append mode the data is stacked in order, not mixed together. This is exactly what we expected.
Part 4 — Adding a Code Node After the Merge (Bonus Step)
Once your data is merged, you can process all 60 items together with a Code node. Here is what we did in our session to build a formatted description for each item.
-
Step 1: Add a Code node after the Merge node and rename it Build Description. Set the mode to Run Once for Each Item and the language to JavaScript.
-
Step 2: Paste in the following code:
const item = $input.item.json; const desc = `[${item.team.toUpperCase()}] #${item.id} - ${item.title} (${item.status || 'N/A'})`; return { json: { ...item, description: desc } }; -
Step 3: Run the workflow. If you see undefined appearing in the description for fields like id or title, that means your upstream Set nodes dropped those fields. Go back to each of the three Label Set nodes (Label Marketing, Label Engineering, Label Sales) and change Include Other Input Fields from Selected to All. This ensures every field from the original item passes through alongside the new team field.
-
Step 4: Re-run after the fix. Your output should now show a clean description field on every item, for example: [MARKETING] #1 - delectus aut autem (N/A).
Bug note worth saving: In n8n, when you add a Set node and only select specific fields to include, n8n drops everything else by default. Always double-check the Include Other Input Fields setting — set it to All unless you specifically want to remove fields. This is a very common gotcha for beginners in any n8n workflow automation project.
Quick Reference: IF vs Switch vs Merge
Here is a simple breakdown to help you decide which node to use:
- IF node — Use when you have exactly two paths: true or false. Great for simple yes/no conditions like "is this task completed?" or "does this email contain SPAM?"
- Switch node — Use when you have three or more paths. Route items based on a value that can match multiple cases. Always handle the Fallback branch for values that do not match any rule.
- Merge node — Use to bring multiple branches back together into one data stream. In Append mode it stacks items in order without mixing them.
Conclusion:
In this n8n IF node tutorial, you learned how to split workflow data into two paths with the IF node, route items across multiple branches with the Switch node, and combine everything back together using the Merge node — all key skills for building real-world n8n workflow automation. Keep these three nodes in mind whenever your automation needs to make decisions, and always check your Set node field settings to avoid the "undefined field" bug we caught in our session.
If you have any questions, feel free to leave a comment below. Thank you!
Tags: n8n tutorial, n8n workflow automation, n8n IF node tutorial, n8n Switch node, n8n Merge node, n8n branching workflow, n8n beginner guide, workflow automation tips
Thank you for your article.
ReplyDeleteThank you so much for taking the time to read our article—we're thrilled it was helpful! We appreciate your support and hope to see you back on QTitHow.com soon for more great content.
Delete