Working with Data &
JSON Transformations
N8N passes data between nodes as a structured array of items. Learn to add fields with the Set node, run custom JavaScript with the Code node, and combine data streams with the Merge node.
📝 N8N's Data Structure — The Items Array
Everything in N8N flows as an array of items. Each item is a JavaScript object with a json property containing your data. Understanding this structure is the foundation of building reliable workflows.
The N8N Items Array Format
// This is how N8N passes data between nodes [ { "json": { "name": "Alice", "email": "alice@example.com", "role": "admin" } }, { "json": { "name": "Bob", "email": "bob@example.com", "role": "user" } } ]
When a node receives this array, it processes each item one at a time by default. If you have 10 items, the node runs 10 times — once for each. This is called "item looping" and it's automatic in N8N.
json wrapper exists because N8N items can also carry binary data (files, images) alongside the JSON. This consistent structure lets N8N handle both types uniformly through the same pipeline.⚙️ The Set Node — Add & Modify Fields
The Set node is your primary tool for adding new fields, renaming existing ones, removing fields, and reshaping your data structure. It's a no-code field mapper — you define what the output should look like and N8N does the rest.
Before and After the Set Node
Set Node Configuration — Common Patterns
// Combine two string fields fullName = {{ $json.firstName + ' ' + $json.lastName }} // Normalize email to lowercase email = {{ $json.email.toLowerCase() }} // Extract year from date string year = {{ new Date($json.createdAt).getFullYear() }} // Static value (no expression needed) source = "n8n-workflow" // Conditional value using ternary status = {{ $json.role === 'admin' ? 'active' : 'pending' }}
💻 The Code Node — Custom JavaScript
When the Set node isn't powerful enough, reach for the Code node. It gives you a full JavaScript environment to transform items with any logic you need — map, filter, reduce, fetch, compute — returning a new items array.
[{ json: {...} }, ...]. If you return a plain object or forget the json wrapper, N8N will throw an error. Each element must be an object with a json key.Example: Transform and Enrich All Items
// Transform all items flowing through this node return items.map(item => ({ json: { // Combine first + last name fullName: item.json.firstName + ' ' + item.json.lastName, // Normalize email to lowercase email: item.json.email.toLowerCase(), // Add a processing timestamp timestamp: new Date().toISOString(), // Calculate a score field score: item.json.points * 1.5, // Keep original email for reference originalEmail: item.json.email } }));
More Code Node Patterns
// Filter items — only keep active users return items .filter(item => item.json.status === 'active') .map(item => ({ json: item.json })); // Deduplicate by email address const seen = new Set(); return items.filter(item => { if (seen.has(item.json.email)) return false; seen.add(item.json.email); return true; }); // Sort items by date descending return [...items].sort((a, b) => new Date(b.json.createdAt) - new Date(a.json.createdAt) );
items array to manipulate — best for sorting, grouping, deduplication. "Run Once for Each Item" processes one item at a time with $input.item — best for per-record transformations.🤝 Merge Node — Combining Data Streams
When your workflow splits into multiple branches (from an IF node, Switch node, or parallel API calls), the Merge node brings them back together. Choose the merge strategy based on what your data needs.
Users from DB
Users from API
Append / Key / Position
Single unified stream
🔄 Data Transformation Flow
A typical data pipeline in N8N flows data through a series of transformations before writing it to a destination. Here's the full picture:
Raw API data
JS transform
Rename fields
Combine streams
Transformed items
Common Transformation Operations
1. What is an "item" in N8N's data model?
2. Which node lets you run custom JavaScript to transform data?
3. Which node combines two separate data streams into one?