Thep Excel

$input.first() – ดึง item แรกจาก input ของ node ปัจจุบัน

$input.first() ดึงเฉพาะ item แรกสุดจากข้อมูลที่เข้ามาใน node ปัจจุบัน ใช้เมื่อต้องการ configuration, header row หรือข้อมูลอ้างอิงที่อยู่ตำแหน่งแรกเสมอ return เป็น item object เดียว (ไม่ใช่ array) ถ้าไม่มี items จะได้ undefined

=$input.first()

By ThepExcel AI Agent
16 December 2025

Function Metrics


Popularity
8/10

Difficulty
2/10

Usefulness
8/10

Syntax & Arguments

=$input.first()

How it works

ดึง configuration หรือ settings จาก item แรกก่อน p…

ดึง configuration หรือ settings จาก item แรกก่อน process items อื่น

Extract header row จาก CSV/Excel ที่แปลงมาเป็น JSO…

Extract header row จาก CSV/Excel ที่แปลงมาเป็น JSON

ดึง metadata หรือ summary information ที่อยู่ item…

ดึง metadata หรือ summary information ที่อยู่ item แรกเสมอ

Validation ว่ามีข้อมูลเข้ามาหรือไม่โดยเช็ค item แร…

Validation ว่ามีข้อมูลเข้ามาหรือไม่โดยเช็ค item แรก

Examples

ดึง config จาก item แรก
{{ $input.first().json.apiKey }}
สมมติ input มี item แรกที่เก็บ config เช่น {apiKey: 'sk-xxxx1234', timeout: 5000} ใช้ $input.first().json.apiKey ดึงค่า API Key ออกมาใช้งานได้ทันที pattern นี้ใช้บ่อยมากเมื่อมี Set node ส่ง config มาก่อนครับ
n8n Formula:

={{ $input.first().json.apiKey }}

Result:

sk-xxxx1234

ดึง header row จากข้อมูล CSV
{{ $input.first().json.headers.join(', ') }}
เมื่อ import CSV แล้วแถวแรกเป็น header ใช้ $input.first() ดึง header ออกมาได้ทันที ไม่ต้องใช้ $input.all()[0] ให้ยุ่งยาก ส่วนตัวผมใช้ pattern นี้บ่อยมากตอนทำ data transformation
n8n Formula:

={{ $input.first().json.headers.join(', ') }}

Result:

Name, Email, Phone

ใช้ร่วมกับ optional chaining ป้องกัน error
{{ $input.first()?.json?.email ?? 'no-email@example.com' }}
ใช้ ?. (optional chaining) ป้องกัน error ถ้าไม่มี items หรือไม่มี field email และใช้ ?? ให้ค่า default ถ้าได้ undefined นี่คือ best practice ที่ควรใช้ใน production workflow ครับ
n8n Formula:

={{ $input.first()?.json?.email ?? 'no-email@example.com' }}

Result:

user@example.com หรือ no-email@example.com

เช็คว่ามี input หรือไม่ก่อน process
{{ $input.first() ? 'Ready to process' : 'No data' }}
ใช้ ternary operator เช็คว่ามี item แรกหรือไม่ ถ้าไม่มีจะได้ undefined ซึ่งเป็น falsy value ใช้ใน IF node หรือ Switch node สำหรับ validation และ error handling ได้ดีครับ
n8n Formula:

={{ $input.first() ? 'Ready to process' : 'No data' }}

Result:

Ready to process

FAQs

ถ้าไม่มี items เข้ามาเลยจะเกิดอะไรขึ้น?

จะได้ undefined กลับมาครับ ถ้าพยายามเข้าถึง .json ต่อจะเกิด error ทันที ดังนั้นควรใช้ optional chaining ($input.first()?.json) หรือเช็คด้วย ternary operator ก่อนเข้าถึงข้อมูลเสมอนะครับ 😅

$input.first() ต่างจาก $input.all()[0] ยังไง?

ผลลัพธ์เหมือนกันครับ แต่ $input.first() อ่านชัดเจนกว่าว่าเราต้องการ item แรก และ handle edge case ดีกว่า ถ้าไม่มี items จะได้ undefined ส่วน $input.all()[0] จะได้ undefined เหมือนกัน แต่โค้ดยาวกว่า

ใช้ใน Python Code node ได้ไหม?

ได้ครับ แต่ใน Python ใช้ _input.first แทน (underscore แทน $) และเป็น property ไม่ใช่ method ดังนั้นไม่ต้องใส่วงเล็บ เช่น first_item = _input.first

Resources & Related

Additional Notes

$input.first() เป็นเมธอดสำหรับดึงข้อมูลรายการแรกสุดจากอินพุตของโหนดปัจจุบัน

ที่เจ๋งคือมันคืนค่าเป็นออบเจ็กต์รายการเดี่ยวๆ ไม่ใช่อาร์เรย์เหมือน $input.all() ทำให้ใช้งานง่ายกว่าเวลาต้องการแค่ข้อมูลชิ้นเดียว ไม่ต้องมานั่งใส่วงเล็บศูนย์อีกทีครับ

ส่วนตัวผมใช้บ่อยมากในรูปแบบที่มีโหนดตั้งค่าส่งคอนฟิกมาก่อน เช่น คีย์เอพีไอ การตั้งค่าต่างๆ แล้วค่อยประมวลผลข้อมูลในโหนดถัดไป การใช้เมธอดนี้ทำให้โค้ดอ่านง่ายและชัดเจนว่าเราต้องการแค่รายการแรกจริงๆ

ที่ต้องระวังคือถ้าไม่มีรายการเลย จะคืนค่าว่างเปล่า ดังนั้นควรเช็คด้วยเครื่องหมายคำถามจุดก่อนเข้าถึงข้อมูลนะครับ 😎

Leave a Reply

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