Complete "Lesson 3: Authentication Methods — API Keys, OAuth2, and Headers" first to unlock this lesson.Go to previous lesson

Introduction

When connecting multiple services in n8n, raw data from one API rarely matches the format expected by the next. You need to reshape fields, filter out irrelevant records, and route data to different branches based on conditions. n8n provides three core nodes for this: Set (for simple field mapping), Function (for custom JavaScript logic), and IF (for conditional routing). Mastering these transforms a linear workflow into a smart, multi-path automation.

Reshaping Data with the Set Node

The Set node lets you create new fields or rename existing ones without writing code. It is the go-to tool for simple transformations like extracting a nested property, combining fields, or adding static values.

Suppose a webhook delivers this payload:

json
{
"user": {
"firstName": "Alice",
"lastName": "Johnson"
},
"orderTotal": 149.99
}

You want to send it to a CRM that expects `full_name` and `amount`. Add a Set node after the webhook, configure two fields: `full_name` with expression `{{json.user.firstName}} {{json.user.lastName}}`, and `amount` with `{{$json.orderTotal}}`. The output becomes:

json
{
"full_name": "Alice Johnson",
"amount": 149.99
}

Set node supports expressions using double curly braces. You can reference `json‘forthecurrentitem,‘json` for the current item, `node['NodeName'].json` for output from another node, and built-in functions like `now‘or‘now` or `today`. For arithmetic or string operations, write plain JavaScript inside the braces: `{{$json.price * 1.1}}` adds 10% markup.

flowchart showing webhook data flowing into Set node, fields being remapped, then output to CRM node

Custom Logic with the Function Node

When Set node expressions become unwieldy, switch to the Function node. It runs arbitrary JavaScript and gives you full control over the input array. The node receives `items` (an array of objects, each with a `json` property) and must return an array in the same structure.

Example: filter out orders below $100 and add a discount tier:

javascript
for (let i = 0; i < items.length; i++) {
const order = items[i].json;
if (order.amount < 100) {
items.splice(i, 1);
i--;
continue;
}
order.discount_tier = order.amount >= 500 ? 'gold' : 'silver';
}
return items;

This modifies the items array in place. For complex transformations, you can map over items and return a new array:

javascript
return items.map(item => ({
json: {
customer_id: item.json.user.id,
total: Math.round(item.json.amount),
tags: item.json.categories.join(', ')
}
}));

Function node has access to Node.js standard library and a few helpers like `$input.all()` to get all incoming items. Avoid async operations unless you understand promise handling; for API calls inside functions, use HTTP Request node instead and chain them.

Conditional Routing with the IF Node

IF node splits your workflow into two branches: true and false. It evaluates one or more conditions against each item and sends matching items down the corresponding path.

Add an IF node after your data source. Set condition type to `Number` if comparing amounts, `String` for text matching, `Boolean` for flags. You can combine multiple conditions with AND/OR logic.

Example workflow: A Typeform webhook sends survey responses. Use IF node with condition `{{$json.score}} > 8`. Connect the true output to a Slack node that posts positive feedback to a channel. Connect the false output to a Google Sheets node that logs issues for review.

javascript
// Condition in IF node:
// Value 1: {{$json.score}}
// Operation: Larger
// Value 2: 8

IF node does not modify data; it only routes. If you need to transform before routing, place a Set or Function node before the IF node. If both branches need the same follow-up action, merge them using a Merge node (choose "Append" mode to combine outputs).

flowchart showing data entering IF node, splitting into true branch going to Slack and false branch going to Google Sheets

Combining Nodes in Multi-Service Workflows

Real workflows chain these nodes. Pattern: Webhook → Function (clean data) → IF (route by type) → Set (format for API A) → HTTP Request (send to API A), and the false branch goes Set (format for API B) → HTTP Request (send to API B).

Common mistake: forgetting that each node processes items independently. If a Function node filters out items, downstream nodes only see the remaining items. If you need original data later, store it in a variable inside the Function node or use a Set node early to copy fields you will need.

Another pitfall: complex expressions in Set node that fail silently. Always test with Execute Workflow and inspect each node's output. If an expression returns undefined, the field is omitted from output. Use `{{$json.field || 'default'}}` to provide fallbacks.

Summary

Set node handles simple field mapping and expressions. Function node gives full JavaScript control for filtering, aggregation, and complex transforms. IF node routes data down different paths based on conditions. Combine them to build workflows that intelligently adapt to incoming data. Always validate transformations by executing the workflow with real sample data and checking intermediate outputs before connecting to live services.

Lesson practice

Lesson Checkpoint

Review what you learned and get feedback on your work.

0/1
Answered · 0/1