n8n Masterclass IModule 2

2.2Data Mapping and JS Expressions

25 min 3 code blocks Practice Lab Homework Quiz (5Q)

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

Technical Snippet: High-Fidelity JS Expressions

Common data transformations you will use:

javascript
// 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] }}
Key Insight

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

Practice Lab: The Data Transformer

  1. Trigger: Create a "Manual Trigger" with a JSON payload containing a messy name (e.g., " taqi NAQVI ").
  2. Transform: Use a "Set" node with a JS expression to trim the whitespace and convert it to Title Case.
  3. 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.

javascript
// 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

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

🎯 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

code
📊 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

Includes hands-on practice labHomework assignment included3 runnable code examples5-question knowledge check below

Quiz: Data Mapping and JS Expressions

5 questions to test your understanding. Score 60% or higher to pass.