Automations 7 min

Transforming Data in n8n

Short answer

In n8n you transform data using three main tools: the Set node for renaming or adding fields, the Edit Fields node for a visual field-mapping interface, and the Code node for complex JavaScript transformations. Use expressions like {{ $json.first_name }} to reference input values. For date formatting and array manipulation, the Code node gives you full JavaScript access, including the luxon date library that n8n bundles by default.

Use the Set node, Edit Fields node, and Code node in n8n to rename, reformat, and restructure data between steps in your workflow.

Why Data Transformation Is Central to Automation

Automation tools connect systems that were built independently, and those systems almost never agree on field names, date formats, or data structures. An API returns first_name and last_name as separate fields, but your CRM expects a single fullName field. A webhook sends dates as Unix timestamps (1720000000), but your database expects ISO 8601 format (2024-07-03T12:26:40Z). A response includes fifty fields but your next step needs only three.

Without transformation, you end up either building custom connectors for each integration or storing data in a format that does not match your system’s expectations. n8n’s transformation nodes let you reshape data in-workflow, between the step that receives it and the step that uses it.

If you are new to n8n, start with creating your first n8n workflow before diving into transformation techniques.

The Set Node: Simple Field Mapping

The Set node is the most straightforward transformation tool. You define a list of output fields, each with a name and a value. The value can be a static string, a number, or an expression that references input data.

To rename first_name to firstName, add a field named firstName with the value {{ $json.first_name }}. To create a combined full name, add a field fullName with the value {{ $json.first_name + ' ' + $json.last_name }}.

The Keep Only Set Fields toggle is important. When enabled, the output item contains only the fields you defined in the Set node. All other fields from the input are dropped. When disabled (the default), all input fields pass through and your new fields are added or overwrite existing ones. Use “Keep Only Set Fields” when you want to reduce a large API response down to only the fields your next step actually needs.

The Code Node: Full JavaScript Control

For transformations that are too complex for expressions, the Code node gives you a full JavaScript environment. It receives the n8n items array and expects you to return a modified version.

The standard pattern looks like this:

return items.map(item => ({
  json: {
    id: item.json.customer_id,
    fullName: item.json.first_name + ' ' + item.json.last_name,
    email: item.json.email_address.toLowerCase(),
  }
}));

This processes every item in the current batch, creates a new JSON object with renamed and transformed fields, and returns the results. The ...item.json spread operator is useful when you want to keep all existing fields and add new ones:

return items.map(item => ({
  json: {
    ...item.json,
    fullName: item.json.first_name + ' ' + item.json.last_name,
  }
}));

The Code node also has access to built-in helpers. The $now variable gives the current timestamp. The luxon library is available for date operations without importing it.

Converting Date Formats

Date format mismatches are among the most common causes of data pipeline failures. APIs return dates in wildly different formats: ISO 8601 (2024-07-03T12:00:00Z), Unix timestamp (1720008000), European notation (03-07-2024), or US notation (07/03/2024).

In the Code node, use luxon (already available in n8n, no import needed) for reliable date parsing and formatting:

const { DateTime } = require('luxon');

return items.map(item => ({
  json: {
    ...item.json,
    // Parse a European date string and output ISO format
    isoDate: DateTime.fromFormat(item.json.date, 'dd-MM-yyyy').toISO(),
    // Parse a Unix timestamp and output a readable label
    dateLabel: DateTime.fromSeconds(item.json.timestamp).toFormat('dd MMMM yyyy'),
  }
}));

Luxon handles timezone conversion, locale-aware formatting, and edge cases like daylight saving time that are unreliable with the native JavaScript Date object.

Working with Arrays

Sometimes a single item contains an array of nested records that you need to process as separate items. The Split Out node handles this: you point it at an array field and it outputs one item per element.

The reverse operation is the Aggregate node: it collects all items in the current branch and combines their values into a single item containing an array. This is useful when your next step expects a batch (for example, a Google Sheets action that writes all rows at once rather than one row per API call).

For error handling in n8n around transformation nodes, the most important setting is enabling “Continue on Error” on the Code node. If one item fails a transformation (for example, a missing required field), the default behaviour is to stop the entire execution. With “Continue on Error” enabled, the failing item is passed to the error branch while the rest continue.

Our tip: Use the “Output Data” panel in the n8n canvas while building your transformation. After each node runs, click on it to see exactly what fields the next step will receive. This live preview removes guesswork from writing expressions and makes it obvious when a field name has an unexpected case or an extra space.

Frequently asked questions

What is the difference between the Set node and the Edit Fields node in n8n?

Both nodes let you create or rename fields, but they have different interfaces. The Set node has been part of n8n for years and uses a list of key-value pairs where you define each output field and its value expression. The Edit Fields node is a newer, more visual interface that shows the incoming fields in a table and lets you rename, reassign, or delete them inline. The Edit Fields node is faster for straightforward remapping of existing fields. The Set node is more explicit and better suited for building new fields from expressions, especially when you want to remove all other fields using the 'Keep Only Set Fields' toggle.

How do I handle a field that sometimes exists and sometimes doesn't?

Use the optional chaining operator in your expression: {{ $json.address?.city ?? 'Unknown' }}. The question mark after `address` means 'only access city if address exists, otherwise return undefined'. The double question mark provides a fallback value if the result is null or undefined. In the Code node you can use the same syntax: `item.json.address?.city ?? 'Unknown'`. This prevents the node from throwing an error when optional fields are missing, which is a common source of workflow failures when your data source is inconsistent.

Can I merge or join data from two different branches of a workflow?

Yes, n8n has a Merge node designed for this. The most common mode is 'Merge by Key': you specify a field name that exists in both branches (such as `customer_id`) and n8n combines the items where the key matches, similar to a SQL JOIN. The output item contains all fields from both branches. There is also a 'Combine' mode that pairs items by index (first item with first item, second with second, and so on), which works when two branches have the same number of items in the same order. For more details on building multi-step workflows, see creating your first n8n workflow.

Want a dashboard or automation built for you?

Stuurboard BI builds custom Power BI dashboards and workflow automations. Pragmatic, fast, and without the fuss.