แปลงข้อมูลใน n8n: Set, Code, Function Node
เรียนรู้การแปลงข้อมูลใน n8n ด้วย Set Node, Code Node และ Function Node พร้อมตัวอย่างจริงที่ใช้บ่อยในทุก Workflow
ใน Workflow จริงๆ ข้อมูลที่รับเข้ามามักไม่อยู่ในรูปแบบที่พร้อมใช้เสมอ บางทีชื่อ Field ไม่ตรง บางทีต้องรวม 2 Field เป็นอันเดียว บางทีต้องคำนวณค่าใหม่ หรือต้องกรองข้อมูลที่ไม่ต้องการออก การแปลงข้อมูล (Data Transformation) จึงเป็นทักษะสำคัญที่ทุกคนที่ใช้ n8n ต้องรู้
n8n มี Node สำหรับแปลงข้อมูลหลายตัว แต่ที่ใช้บ่อยที่สุดคือ Set Node, Code Node และการใช้ Expression ใน Node ทั่วไป บทความนี้จะอธิบายแต่ละตัวพร้อมตัวอย่างการใช้งานจริง
Set Node — แปลงข้อมูลแบบไม่ต้องเขียนโค้ด
Set Node เป็นเครื่องมือที่ใช้บ่อยที่สุดในการแปลงข้อมูล ใช้ได้ทั้งกำหนดค่าใหม่, เปลี่ยนชื่อ Field, รวม Field หรือลบ Field ที่ไม่ต้องการ
วิธีใช้ Set Node:
- เพิ่ม Set Node ใน Workflow
- เลือก Mode: Manual Mapping หรือ JSON
- กำหนด Field ที่ต้องการ Output
ตัวอย่างที่ 1: รวม First Name และ Last Name
full_name = {{ $json.first_name }} {{ $json.last_name }}
ตัวอย่างที่ 2: เปลี่ยน Format วันที่
formatted_date = {{ $json.timestamp.toDateTime().format('dd/MM/yyyy') }}
ตัวอย่างที่ 3: คำนวณ Field ใหม่
total_with_vat = {{ $json.subtotal * 1.07 }}
discount_percent = {{ ($json.discount / $json.subtotal * 100).toFixed(2) }}
ตัวอย่างที่ 4: ใช้ Conditional Expression
status_thai = {{ $json.status === 'active' ? 'ใช้งานอยู่' : 'ปิดใช้งาน' }}
ตั้งค่า “Keep Only Set” เพื่อลด Field: ถ้าต้องการ Output เฉพาะ Field ที่กำหนด ให้เปิด Keep Only Set Option ใน Set Node วิธีนี้ช่วยให้ข้อมูลที่ส่งต่อไปสะอาดและมีเฉพาะที่จำเป็น
Expression ใน n8n — เปลี่ยนค่าตรงจาก Node
ไม่จำเป็นต้องใช้ Set Node เสมอ ทุก Node ใน n8n รองรับ Expression ใน Field ต่างๆ โดยใช้ {{ }} ครอบ
Expression พื้นฐานที่ควรรู้:
// ดึงค่าจาก Node ก่อนหน้า
{{ $json.field_name }}
// ดึงค่าจาก Node ที่ระบุชื่อ
{{ $('My Node').item.json.field_name }}
// วันเวลาปัจจุบัน
{{ $now.toISO() }}
{{ $now.format('YYYY-MM-DD') }}
// แปลง String เป็น Number
{{ parseInt($json.price) }}
{{ parseFloat($json.rating) }}
// แปลง Number เป็น String
{{ $json.count.toString() }}
// ตัดช่องว่างหน้า-หลัง
{{ $json.name.trim() }}
// แปลงเป็น Lowercase/Uppercase
{{ $json.email.toLowerCase() }}
{{ $json.code.toUpperCase() }}
Code Node — เขียน JavaScript เมื่อ Logic ซับซ้อน
เมื่อ Set Node และ Expression ไม่เพียงพอ Code Node ให้เขียน JavaScript ได้เต็มที่ รองรับ Loop, Condition ซับซ้อน, การ Parse ข้อมูล และการ Format ผลลัพธ์ที่ต้องการ
โครงสร้าง Code Node:
// รับข้อมูลจาก Node ก่อนหน้า
const items = $input.all();
// Process ข้อมูล
const result = items.map(item => {
const data = item.json;
return {
json: {
// กำหนดข้อมูลที่ต้องการ Output
id: data.id,
full_name: `${data.first_name} ${data.last_name}`,
email: data.email.toLowerCase().trim(),
total: data.price * data.quantity,
}
};
});
return result;
ตัวอย่างที่ 1: Parse JSON ที่ซ้อนกัน
const items = $input.all();
return items.map(item => {
// ข้อมูลดิบอาจเป็น String JSON
const raw = typeof item.json.metadata === 'string'
? JSON.parse(item.json.metadata)
: item.json.metadata;
return {
json: {
...item.json,
city: raw.address?.city ?? 'Unknown',
zip: raw.address?.zip ?? '',
}
};
});
ตัวอย่างที่ 2: Group ข้อมูลตาม Category
const items = $input.all();
const grouped = {};
for (const item of items) {
const cat = item.json.category;
if (!grouped[cat]) grouped[cat] = [];
grouped[cat].push(item.json);
}
return Object.entries(grouped).map(([category, records]) => ({
json: { category, count: records.length, records }
}));
ตัวอย่างที่ 3: Format ตัวเลขเงินบาทไทย
const items = $input.all();
return items.map(item => ({
json: {
...item.json,
price_formatted: new Intl.NumberFormat('th-TH', {
style: 'currency',
currency: 'THB'
}).format(item.json.price)
}
}));
เทคนิค: แปลง Array เป็น Items หลายชิ้น
บางครั้ง Node ก่อนหน้าส่งมาเป็น 1 Item ที่มี Array อยู่ข้างใน แต่ต้องการแยกเป็นหลาย Items เพื่อ Loop ผ่าน
const items = $input.all();
const result = [];
for (const item of items) {
// สมมติ item.json.orders เป็น Array
for (const order of item.json.orders) {
result.push({ json: order });
}
}
return result;
วิธีนี้ทำให้ Node ถัดไปรับ Item แยกกัน ไม่ใช่ Array ทั้งก้อน
เปรียบเทียบ: ควรใช้ตัวไหน?
| สถานการณ์ | Node ที่แนะนำ |
|---|---|
| เปลี่ยนชื่อ Field หรือรวม Field | Set Node |
| คำนวณค่าง่ายๆ | Expression ใน Set Node |
| Format วันที่หรือตัวเลข | Expression ใน Node ปลาย |
| Parse JSON ซ้อนกัน | Code Node |
| Loop ซับซ้อน, Group ข้อมูล | Code Node |
| แปลง Array เป็นหลาย Items | Code Node |
ข้อผิดพลาดที่พบบ่อย
ปัญหา: Expression แสดง undefined
มักเกิดเมื่อ Field ที่อ้างอิงไม่มีอยู่ใน Input ให้ตรวจสอบชื่อ Field ใน Input Panel และใช้ ?? Operator สำหรับค่า Default เช่น {{ $json.name ?? 'ไม่ระบุ' }}
ปัญหา: Code Node ไม่ Return ข้อมูล
Code Node ต้อง Return Array ของ Object ที่มี json Property เสมอ ถ้าลืม Return หรือ Return ผิดรูปแบบ Node ถัดไปจะไม่ได้ข้อมูล
ปัญหา: วันที่แสดงผิด Timezone
n8n เก็บเวลาเป็น UTC โดย Default ถ้าต้องการเวลาไทย (UTC+7) ให้บวก 7 ชั่วโมง หรือใช้ .setTimezone('Asia/Bangkok') ใน Expression
ปัญหา: ตัวเลขทศนิยมผิดพลาด
JavaScript มีปัญหา Floating Point เช่น 0.1 + 0.2 !== 0.3 ถ้าต้องการความแม่นยำในการคำนวณเงิน ให้คูณ 100 ก่อนคำนวณแล้วหาร 100 ทีหลัง หรือใช้ Library อย่าง decimal.js
อยากเรียน n8n แบบเป็นระบบ ตั้งแต่เริ่มต้นจนสร้าง Workflow ใช้งานจริงได้ ลองดู คอร์สสอน n8n ที่ aiunlock.co
Related posts
n8n Advanced: 10 เทคนิคขั้นสูงสำหรับมืออาชีพ
รวม 10 เทคนิค n8n ขั้นสูงที่มืออาชีพใช้จริง ตั้งแต่ Error Handling, Sub-Workflow, Code Node ไปจนถึง API Pagination
สร้าง AI Workflow ด้วย n8n: จาก ChatGPT ถึง AI Agent
คู่มือครบจบ สร้าง AI Workflow ด้วย n8n ตั้งแต่เชื่อม ChatGPT, Claude, Gemini ไปจนถึงสร้าง AI Agent อัจฉริยะ
n8n x Airtable: ใช้ Airtable เป็น Backend ง่ายๆ
เชื่อมต่อ n8n กับ Airtable เพื่ออ่าน เพิ่ม อัปเดต และลบข้อมูลอัตโนมัติ เหมาะสำหรับทีมที่ไม่มี Dev