๐ŸŽ“Iris Courses
โ† AI Automation Mastery
Day 5 of 21

Data Transformation Fundamentals

Why Data Transformation Is the Hard Part

Here's a truth no automation tutorial tells you: connecting two systems is easy. The hard part is transforming the data from what system A sends into what system B expects. This mismatch is responsible for 80% of automation failures. System A sends a date as '2024-01-15T14:30:00Z'. System B wants 'January 15, 2024'. System A sends a name as one field: 'John Smith'. System B wants separate 'firstName' and 'lastName' fields. System A sends price in cents (1500). System B wants dollars with two decimal places ('15.00'). These are trivial examples โ€” real-world mismatches are far messier. In n8n, you have several tools for data transformation. The Set node lets you create and remap fields. The Function node (or Code node) lets you write JavaScript to manipulate data any way you want. The Item Lists node helps you work with arrays โ€” splitting, merging, deduplicating. The Date & Time node handles time zone conversions and format changes. The key mental model: always inspect your raw trigger data first (use n8n's test execution feature) before building transformation nodes. You need to understand the exact structure of what you're working with. Don't assume the field names or data types โ€” verify them. A data mismatch caught in development takes 5 minutes to fix. A data mismatch caught when your automation has been silently inserting malformed data into your CRM for two weeks is a serious problem.

JavaScript in n8n: The Code Node

The Code node is where n8n goes from 'no-code tool' to 'developer-grade automation platform'. You can write full JavaScript inside any workflow, with access to all incoming data. Basic Code node usage: the incoming items are available as `$input.all()`. You return an array of items. Here's a template: ```javascript const items = $input.all(); return items.map(item => { const data = item.json; return { json: { fullName: data.firstName + ' ' + data.lastName, email: data.email.toLowerCase().trim(), signupDate: new Date(data.created_at).toLocaleDateString('en-AU'), isHighValue: data.plan === 'enterprise' || data.mrr > 1000 } }; }); ``` Powerful Code node patterns: parsing JSON strings that arrive as text fields, extracting data with regex (phone numbers, URLs, order IDs from email bodies), calculating derived fields (days since signup, percentage change), filtering arrays by multiple conditions, and reformatting nested objects. One advanced pattern worth knowing: you can make HTTP requests from inside the Code node using the built-in `$http` helper or fetch. This lets you call additional APIs inline without adding extra nodes, useful for quick lookups. Build the habit of always adding error handling. Wrap your Code node logic in try/catch and return a structured error object when things go wrong โ€” this makes debugging production workflows dramatically easier.

โšก Today's Action

Take one of your existing workflows (or the lead capture from Day 3) and add a Code node that transforms the data in at least 3 ways: normalize the email to lowercase, format the date in a human-readable format, and add a calculated field.

๐Ÿ’ก Pro Tip

In n8n's Code node, use `console.log(JSON.stringify($input.all(), null, 2))` to dump the full incoming data structure during development. Remove these logs before production โ€” they create noise in your execution logs.