Overview
In this lesson, you will learn how to use the Merge node in n8n to combine data from different streams effectively. Specifically, you will see how to merge order header data with order line item details, enabling you to work with a unified dataset for further automation tasks. This tutorial covers the different merge modes, how to match records by fields, and practical use cases in business workflows.
Understanding the Merge Node in n8n
The Merge node in n8n allows you to combine data coming from multiple input streams into a single output. This is essential when you want to consolidate data from different sources or different parts of a workflow to process it uniformly.
If you're new to how data flows in n8n, reviewing Understanding Data in n8n will give you a solid foundation on data concepts that will help you grasp the merge process better.
Why Use the Merge Node?
- Combine related data from different sources (e.g., orders and their details)
- Aggregate multiple datasets into one for further processing
- Clean and prepare data by filtering or joining relevant records
- Build complex workflows that require synchronized data inputs
Practical Use Case: Merging Order Headers with Order Details
Imagine you have two datasets:
- Order Headers: Contains order IDs, customer names, emails, order status, and dates.
- Order Details: Contains order line items with product IDs, quantities, prices, etc.
Your goal is to merge these two datasets so each order header is combined with its relevant product line items.
Step 1: Setup Your Workflow Inputs
- Use a
Manual Triggernode to start the workflow. - Add two
Google Sheetsnodes:- One to fetch Order Headers (e.g., 100 records)
- Another to fetch Order Details (e.g., 291 records)
Make sure each node is configured to fetch the correct sheet:
Google Sheets Node 1: Get Orders Header Data
- Sheet Name: mock orders data
- Output: Order ID, First Name, Last Name, Email, Status, Order Date
Google Sheets Node 2: Get Order Details Data
- Sheet Name: mock order details data
- Output: Order ID, Product ID, Product Name, Category, Quantity, Unit Price, Total Price
Step 2: Add the Merge Node
- Add a
Mergenode to your workflow. - Connect the output of both Google Sheets nodes to the
Mergenode inputs. - Open the
Mergenode settings.
For more details on the node itself, you can refer to the Merge Node in n8n lesson.
Configuring the Merge Node
Inputs and Execution Order
- The left input is Input 1.
- The right input is Input 2.
- n8n executes workflow branches from left to right, top to bottom, so arrange your nodes accordingly.
Merge Modes
The Merge node supports several modes for combining data:
| Mode | Description |
|---|---|
| Append | Concatenates data from all inputs one after another. |
| Combine | Merges matching items based on specified field(s). |
| Keep Matches | Returns only matched records (inner join). |
| Keep Non-Matches | Returns only unmatched records (records with no matching counterpart). |
| Keep Everything | Returns all records, matched and unmatched (outer join). |
| Enrich Input 1 | Left join, enriches input 1 with matching data from input 2. |
| Enrich Input 2 | Right join, enriches input 2 with matching data from input 1. |
Step 3: Choose Merge Mode and Configure Matching Fields
For this use case, select:
- Mode:
Combine - Fields to Match:
order ID(drag and drop or type the field name)
This tells n8n to merge records where the order ID matches in both datasets.
You can learn more about how n8n handles data linking and mapping in the official n8n Merge node documentation{:target="_blank"}.
Step 4: Select Output Type
Choose the output type based on your desired result:
- Keep Matches: Only orders with matching details are returned.
- Keep Non-Matches: Only records without matches in the other stream are returned.
- Keep Everything: Combines all records regardless of matches.
- Enrich Input 1: Returns all Input 1 records, enriched with matching Input 2 data.
- Enrich Input 2: Returns all Input 2 records, enriched with matching Input 1 data.
Testing Different Output Types
You can simulate different real-world scenarios by manipulating your data:
- Add some orders without details.
- Add some details without corresponding orders.
Then run the workflow with each output type to see how the data changes:
Example Results
| Output Type | Result |
|---|---|
| Keep Matches | Only orders with matching details are shown (e.g., 291 records). |
| Keep Non-Matches | Shows only orders or details without a match (useful for identifying data inconsistencies). |
| Keep Everything | Combines all matched and unmatched records (e.g., 300+ records). |
| Enrich Input 1 | All order headers with matching details included; unmatched orders are preserved. |
| Enrich Input 2 | All order details with matching headers included; unmatched details are preserved. |
Example: Merge Node JSON Configuration Snippet
Here is an example snippet of the Merge node configuration for combining orders on order ID with keep matches option:
{
"parameters": {
"mode": "combine",
"fieldsToMatch": [
"order ID"
],
"output": "keepMatches"
},
"name": "Merge Orders Data",
"type": "n8n-nodes-base.merge",
"typeVersion": 1,
"position": [600, 300]
}
Common Mistakes and Troubleshooting
Mismatched Field Names:
Make sure the field names you use to match records are exactly the same in both inputs. If they differ, use the option to specify different field names for each input.Incorrect Execution Order:
n8n executes branches from left to right, top to bottom. If your inputs must be processed in a certain order, rearrange the nodes accordingly.Choosing the Wrong Mode:
Usingappendmode when you want to merge by matching fields will result in a concatenated list instead of merged records.Data Type Differences:
Ensure that matching fields (e.g., order IDs) have the same data type and formatting in both datasets to avoid failed matches.Large Data Volumes:
Usingcombinemode without matching fields can create a Cartesian product, multiplying the number of records exponentially and slowing down your workflow.
If you need to group or aggregate data before merging, consider using the Aggregate Node in n8n to prepare your datasets efficiently.
Additional Tips
- Use
Mergenode in combination with other transformation nodes likeSetorAggregatefor more advanced data cleaning and shaping. - Test with small datasets first to verify your merge logic before scaling up.
- Use
Remove Duplicatesnode after merging if your workflow risks producing duplicate records.
Further Reading
- Learn more about the Merge node in n8n{:target="_blank"}
- Explore best practices for working with data in n8n{:target="_blank"}
Quick Reference: Merge Node Cheat Sheet
| Setting | Description | Recommended Value for Merging Orders |
|---|---|---|
| Mode | How to combine data streams | Combine |
| Fields to Match | Field(s) on which to join records | order ID |
| Output Type | Defines which records to include in output | Keep Matches or Enrich Input 1 |
| Number of Inputs | Number of input streams to merge | 2 (or more if needed) |
| Field Name Mapping | Map different field names in inputs if they don't match | Enable and specify accordingly |
By mastering the Merge node, you can effectively combine and clean your data streams for robust and scalable automation workflows in n8n. As shown in the video above, this approach is fundamental for real-world business automation scenarios where data integrity and completeness are critical.