การดู : 211

12/04/2026 18:17น.

JS2GO EP.22 การทำงานกับ JSON — JavaScript กับ Go ต่างกันอย่างไร?

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

 

แนวทางปฏิบัติที่เหมาะสม

 

  1. ตรวจสอบ error ทุกครั้ง
    • JavaScript: ใช้ try/catch
    • Go: ตรวจสอบ err หลังการ marshal/unmarshal
  2. เลือก data structure ให้เหมาะสม
    • Go: ใช้ struct สำหรับ JSON ที่คาดเดา structure ได้
    • JavaScript: ใช้ object หรือ type annotation (TypeScript)
  3. จัดการค่า field ที่อาจไม่มีค่า
    • Go: ใช้ pointer หรือ omitempty tag
    • JavaScript: เช็คค่า undefined ก่อนใช้งาน
  4. แปลงและ serialize อย่างปลอดภัย
    • JavaScript: หลีกเลี่ยง JSON.stringify กับ circular reference
    • Go: ใช้ json.Decoder สำหรับ streaming JSON ขนาดใหญ่

 


 

สรุปเปรียบเทียบ JSON JavaScript vs Go

 

FeatureJavaScriptGo
ParsingJSON.parsejson.Unmarshal
SerializationJSON.stringifyjson.Marshal
Type checkingDynamicStatic (struct)
Error handlingtry/catchreturn error
Optional fieldsundefined / deletepointer + 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/