2.2 — Data Mapping and JS Expressions
Data Mapping and JS Expressions: The Logic Layer
In 2026, the power of n8n lies in its ability to manipulate data between nodes using JavaScript expressions. This lesson teaches you how to transform raw JSON payloads into clean, actionable variables for your growth engine.
🏗️ The Expression Syntax
n8n uses a template-string-like syntax to access data from previous nodes.
- Accessing Data:
{{ $node["NodeName"].json["field_name"] }} - Accessing Parameters:
{{ $node["NodeName"].parameter["param_name"] }}
Technical Snippet: High-Fidelity JS Expressions
Common data transformations you will use:
// 1. Normalize a phone number to E.164 format
{{ $json.phone.replace(/[^0-9]/g, '').replace(/^0/, '+92') }}
// 2. Conditional text based on lead score
{{ $json.score > 8 ? "Priority Lead" : "Standard Lead" }}
// 3. Extracting the domain from an email
{{ $json.email.split('@')[1] }}
Nuance: The 'Binary' vs 'JSON' Data
n8n distinguishes between JSON data (text/numbers) and Binary data (files/images). When mapping an AI-generated report to an email node, you must ensure you are referencing the json key, not the binary output.
Practice Lab: The Data Transformer
- Trigger: Create a "Manual Trigger" with a JSON payload containing a messy name (e.g.,
" taqi NAQVI "). - Transform: Use a "Set" node with a JS expression to trim the whitespace and convert it to Title Case.
- Result: Verify the output is clean and professional.
🇵🇰 Pakistan Example: Phone Number Normalization
Pakistani phone numbers come in chaos: 03001234567, +923001234567, 0300-123-4567, 92300 1234567. Your automation needs to handle ALL of these.
// Universal PK phone normalizer for n8n Code node
{{ $json.phone.replace(/[\s\-\(\)]/g, '').replace(/^0/, '+92').replace(/^92/, '+92') }}
Why this matters: When you're syncing leads from a Karachi agency's Google Sheet to their WhatsApp Business API (WATI), misformatted numbers = failed messages = lost revenue. This one expression saves hours of manual cleanup.
Homework: The Revenue Calculator
Create a workflow that takes a "Deal Size" in PKR and a "Closing Probability" (0-1). Use a JS expression to calculate the "Expected Value" and format it as "PKR 500,000". Bonus: Add a USD conversion using a fixed rate expression.
📺 Recommended Videos & Resources
- n8n Expressions & Data Mapping Tutorial — Official n8n expression language documentation
- Type: Documentation
- Link description: Visit docs.n8n.io, search "Expressions"
- JavaScript in n8n Code Nodes (Deep Dive) — YouTube tutorial on writing custom JS transformations
- Type: YouTube
- Link description: Search YouTube for "n8n JavaScript code node tutorial"
- Phone Number Validation & Formatting (Regex) — Learn regex patterns for phone/email normalization
- Type: YouTube
- Link description: Search YouTube for "regex for phone numbers validation"
- n8n Set Node vs Code Node (When to Use Each) — Optimization guide for performance
- Type: YouTube
- Link description: Search YouTube for "n8n Set vs Code node comparison"
- Data Type Conversion in n8n (String/Number/Date) — Prevent "cannot add property to undefined" errors
- Type: Documentation
- Link description: Visit docs.n8n.io/data/data-mapping
🎯 Mini-Challenge
Master phone normalization: Write a JS expression that converts 10 different Pakistani phone number formats (like the ones in the lesson) into a single standardized format. Test it with real numbers from your contacts!
🖼️ Visual Reference
📊 Data Transformation Pipeline
Raw Webhook Input:
┌─────────────────────────────────┐
│ { │
│ "phone": "03001234567", │
│ "name": " TAQI ", │
│ "score": "8.5" │
│ } │
└──────────────┬──────────────────┘
│
↓ JS Expression
┌──────────────────┐
│ Set Node / Code │
│ Transform Data │
└─────────┬────────┘
│
Clean Output:
┌─────────────────────────────────┐
│ { │
│ "phone": "+923001234567", │
│ "name": "Taqi", │
│ "score": 8.5, │
│ "priority": "Priority Lead" │
│ } │
└─────────────────────────────────┘
Ready for CRM, Email, or WhatsApp APIs
Lesson Summary
Quiz: Data Mapping and JS Expressions
5 questions to test your understanding. Score 60% or higher to pass.