Thep Excel

$json – ตัวแปรสำหรับเข้าถึงข้อมูล JSON ของ item ปัจจุบัน

$json เป็นตัวแปรพื้นฐานที่สำคัญที่สุดใน n8n ใช้เข้าถึงข้อมูล JSON ของ item ปัจจุบันที่กำลังประมวลผล ทำงานคล้าย this หรือ current row ใน Excel สามารถเข้าถึงข้อมูลได้ทั้งแบบ dot notation ($json.field) และ bracket notation ($json[‘field’]) ใช้บ่อยที่สุดในทุก workflow เพราะเป็นจุดเริ่มต้นของการเข้าถึงข้อมูล

=$json

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
10/10

Difficulty
2/10

Usefulness
10/10

Syntax & Arguments

=$json

How it works

เข้าถึงข้อมูลจาก API response ที่ได้รับจาก HTTP Re…

เข้าถึงข้อมูลจาก API response ที่ได้รับจาก HTTP Request node

ดึงค่าจากฐานข้อมูลที่ query มาจาก database node

ดึงค่าจากฐานข้อมูลที่ query มาจาก database node

อ่านข้อมูลจาก webhook ที่ส่งเข้ามาในรูปแบบ JSON

อ่านข้อมูลจาก webhook ที่ส่งเข้ามาในรูปแบบ JSON

ประมวลผลข้อมูลที่ได้จาก node ก่อนหน้าในทุก workflo…

ประมวลผลข้อมูลที่ได้จาก node ก่อนหน้าในทุก workflow

Examples

เข้าถึงข้อมูลด้วย dot notation
{{ $json.name }}
เข้าถึงฟิลด์ name ของ item ปัจจุบัน เหมาะกับฟิลด์ที่ชื่อไม่มีช่องว่างหรืออักขระพิเศษ วิธีนี้อ่านง่ายและใช้บ่อยที่สุด
n8n Formula:

={{ $json.name }}

Result:

John

เข้าถึงข้อมูลแบบ bracket notation
{{ $json['body']['city'] }}
เข้าถึงฟิลด์ nested โดยใช้ bracket notation เหมาะกับฟิลด์ที่มีช่องว่างหรืออักขระพิเศษในชื่อ เช่น 'First Name' หรือ 'user-id'
n8n Formula:

={{ $json['body']['city'] }}

Result:

Bangkok

ใช้ร่วมกับ string method
{{ $json.email.toLowerCase().trim() }}
เชื่อมต่อ method ของ JavaScript เพื่อทำ data cleaning สามารถใช้ trim(), toLowerCase(), replace() และอื่นๆ ได้ทันที pattern นี้ใช้บ่อยมากในการทำความสะอาดข้อมูล
n8n Formula:

={{ $json.email.toLowerCase().trim() }}

Result:

user@example.com

ตรวจสอบค่าว่างด้วย conditional
{{ $json.status ? $json.status : 'pending' }}
ใช้ ternary operator เพื่อให้ค่า default กรณีที่ field เป็น null หรือ undefined ป้องกัน error จากข้อมูลไม่ครบ pattern นี้สำคัญมากในการจัดการข้อมูลจริง
n8n Formula:

={{ $json.status ? $json.status : 'pending' }}

Result:

active

FAQs

ต่างจาก $input.item อย่างไร

$json เป็น shortcut ที่เข้าถึงเฉพาะส่วน JSON data ของ item ส่วน $input.item คือ item object ทั้งหมด ที่รวมทั้ง json, binary, และ pairing info ใช้ $json เมื่อต้องการเข้าถึงข้อมูลอย่างเดียว (ใช้บ่อยกว่า 95%)

ทำไมต้องใช้ {{ }} ครอบ expression

n8n ใช้ {{ }} เป็นเครื่องหมายบอกว่านี่คือ expression ที่ต้อง evaluate ไม่ใช่ plain text ถ้าไม่ครอบจะถือเป็น string ธรรมดา เหมือนกับ template engine อื่นๆ เช่น Handlebars

เวลาไหนควรใช้ dot notation vs bracket notation

ใช้ dot notation ($json.name) เมื่อชื่อฟิลด์เป็น valid JavaScript identifier (ไม่มีช่องว่าง เครื่องหมายพิเศษ) ใช้ bracket notation ($json[‘First Name’]) เมื่อชื่อฟิลด์มีช่องว่างหรืออักขระพิเศษ หรือเมื่อชื่อฟิลด์เป็นตัวแปร

ถ้าฟิลด์ไม่มีจะเกิดอะไรขึ้น

จะได้ undefined กลับมา ไม่ error ทันที แต่ถ้าพยายามเข้าถึง property ต่อของ undefined จะ error ดังนั้นควรใช้ optional chaining ($json?.field?.nested) หรือ ternary operator เพื่อป้องกัน

Resources & Related

Additional Notes

เทคนิคสำคัญ: ใช้ $json เป็นจุดเริ่มต้นเสมอเมื่อต้องการเข้าถึงข้อมูล ถ้าต้องการเข้าถึงข้อมูลจาก node อื่น ใช้ $node[\”NodeName\”].json แทน สามารถ chain method ของ JavaScript ได้ทันทีเช่น .trim(), .toLowerCase(), .split() เพราะ n8n รองรับ JavaScript expression ครบ สำหรับข้อมูล nested ลึกหลายชั้น แนะนำใช้ bracket notation เพื่อความชัดเจน และควรเช็ค null/undefined ก่อนเข้าถึงข้อมูลลึกเพื่อป้องกัน error ใน production

Leave a Reply

Your email address will not be published. Required fields are marked *