12/04/2026 18:17น.

JS2GO EP.22 การทำงานกับ JSON — JavaScript กับ Go ต่างกันอย่างไร?
#JS2GO
#JSON
#JavaScript
#Go
#การจัดการ JSON
JSON (JavaScript Object Notation) เป็นรูปแบบข้อมูลที่นิยมใช้สำหรับการแลกเปลี่ยนข้อมูลระหว่างระบบ โดยเฉพาะใน API และ Web Services การจัดการ JSON อย่างถูกต้องช่วยให้ข้อมูลถูกต้อง ป้องกัน runtime error และทำให้ระบบทำงานได้อย่างราบรื่น ในบทความนี้เราจะเปรียบเทียบการ อ่าน (Parse), เขียน (Serialize) และ แปลง JSON ใน JavaScript (Node.js) และ Go พร้อมตัวอย่างโค้ดและแนวทางปฏิบัติที่เหมาะสม
การจัดการ JSON ใน JavaScript
JavaScript มี Object Built-in JSON สำหรับการ parse และ stringify ข้อมูล JSON
Parsing JSON (String → Object)
const jsonString = '{"name": "Boom", "age": 28}';
try {
const obj = JSON.parse(jsonString);
console.log(obj.name); // Boom
console.log(obj.age); // 28
} catch (err) {
console.error('Error parsing JSON:', err);
}
Serializing Object → JSON String
const user = { name: 'Boom', age: 28 };
const jsonStr = JSON.stringify(user);
console.log(jsonStr); // {"name":"Boom","age":28}
ข้อดีของ JavaScript
- JSON built-in ใช้งานง่าย
- Dynamic typing ทำให้แก้ไข object ได้ทันที
- Async/await + fetch ทำงานกับ API JSON ได้สะดวก
ข้อจำกัด
- ไม่มี type checking หาก JSON ผิดรูปแบบอาจเกิด runtime error
- ต้อง handle error ด้วย try/catch
การจัดการ JSON ใน Go
Go มี package encoding/json สำหรับจัดการ JSON
Parsing JSON (String → Struct)
package main
import (
"encoding/json"
"fmt"
"log"
)
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
jsonString := `{"name": "Boom", "age": 28}`
var user User
err := json.Unmarshal([]byte(jsonString), &user)
if err != nil {
log.Fatal(err)
}
fmt.Println(user.Name) // Boom
fmt.Println(user.Age) // 28
}
Serializing Struct → JSON String
user := User{Name: "Boom", Age: 28}
jsonData, err := json.Marshal(user)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(jsonData)) // {"name":"Boom","age":28}
ข้อดีของ Go
- Type-safe ตรวจสอบ field และ type ขณะ compile
- รองรับ struct tags สำหรับ mapping JSON key
- Error handling ชัดเจน
ข้อจำกัด
- Syntax verbose สำหรับงานง่าย ๆ
- ต้องกำหนด struct ให้ตรงกับ JSON structure
แนวทางปฏิบัติที่เหมาะสม
- ตรวจสอบ error ทุกครั้ง
- JavaScript: ใช้
try/catch - Go: ตรวจสอบ
errหลังการ marshal/unmarshal
- JavaScript: ใช้
- เลือก data structure ให้เหมาะสม
- Go: ใช้
structสำหรับ JSON ที่คาดเดา structure ได้ - JavaScript: ใช้ object หรือ type annotation (TypeScript)
- Go: ใช้
- จัดการค่า field ที่อาจไม่มีค่า
- Go: ใช้ pointer หรือ
omitemptytag - JavaScript: เช็คค่า
undefinedก่อนใช้งาน
- Go: ใช้ pointer หรือ
- แปลงและ serialize อย่างปลอดภัย
- JavaScript: หลีกเลี่ยง
JSON.stringifyกับ circular reference - Go: ใช้
json.Decoderสำหรับ streaming JSON ขนาดใหญ่
- JavaScript: หลีกเลี่ยง
สรุปเปรียบเทียบ JSON JavaScript vs Go
| Feature | JavaScript | Go |
|---|---|---|
| Parsing | JSON.parse | json.Unmarshal |
| Serialization | JSON.stringify | json.Marshal |
| Type checking | Dynamic | Static (struct) |
| Error handling | try/catch | return error |
| Optional fields | undefined / delete | pointer + omitempty |
คำแนะนำ
- สำหรับ rapid prototyping หรือ front-end ใช้ JavaScript จะสะดวก
- สำหรับ backend service / API / data validation ใช้ Go จะมั่นใจและ type-safe
ตอนต่อไป
ใน EP.23 ของซีรีส์ JS2GO เราจะพาคุณไปเรียนรู้ การทำงานกับ Date และ Time ใน JavaScript และ Go พร้อมตัวอย่างโค้ดและแนวทางปฏิบัติที่เหมาะสม เพื่อจัดการเวลาในระบบให้ถูกต้องและแม่นยำ
อ่านบทความ Series อื่นๆ
🔵 Facebook: https://www.facebook.com/superdev.academy.th
🔴 YouTube : Superdev Academy
📸 Instagram: Superdev Academy
🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH
🌐 Website: https://www.superdevacademy.com/