ในบทความนี้ เราจะไปดูวิธีการใช้ 4 functions จาก jsonlite package สำหรับทำงานกับ JSON (JavaScript Object Notation) ในภาษา R กัน:
fromJSON()toJSON()read_json()write_json()
ถ้าพร้อมแล้ว ไปเริ่มกันเลย
- 🤔 What Is JSON?
- 📦 What Is jsonlite?
- 🏁 Getting Started
- ⬇️ Function 1. fromJSON()
- ⬆️ Function 2. toJSON()
- 📖 Function 3. read_json()
- ✍️ Function 4. write_json()
- 💪 Summary
- 😺 GitHub
- 📃 References
🤔 What Is JSON?
JSON เป็นโครงสร้างข้อมูลที่พบได้บ่อยในระบบคอมพิวเตอร์และแอปพลิเคชันต่าง ๆ เพราะเป็นรูปแบบข้อมูลที่อ่านได้ง่ายทั้งคนและคอมพิวเตอร์
ตัวอย่าง JSON ที่เก็บข้อมูลขนม:
[
{
"snack_name": "Corn Chips",
"brand": "Nibble & Dip",
"price_usd": 2.99,
"is_vegan": true
},
{
"snack_name": "Pita Chips",
"brand": "DipMates",
"price_usd": 4.25,
"is_vegan": false
},
{
"snack_name": "Tortilla Chips",
"brand": "Casa Crunch",
"price_usd": 3.49,
"is_vegan": true
}
]
Note: อ่านเพิ่มเติมเกี่ยวกับ JSON ได้ที่ What is JSON?
📦 What Is jsonlite?
jsonlite เป็น package ขนาดเบาสำหรับทำงานกับ JSON ในภาษา R และมี 4 functions หลักให้เรียกใช้งาน ได้แก่:
fromJSON(): โหลด JSON string (character ที่มีข้อมูลแบบ JSON) เป็น R object (เช่น data frame)toJSON(): เปลี่ยน R object เป็น JSON stringread_json(): โหลดไฟล์ JSON เป็น R objectwrite_json(): เปลี่ยน R object เป็นไฟล์ JSON
เราไปดูวิธีใช้ jsonlite และ 4 functions นี้กัน
🏁 Getting Started
ก่อนเริ่มใช้งาน jsonlite เราจะต้องทำ 2 อย่าง ได้แก่:
- ติดตั้ง package ด้วย
install.packages()(ทำครั้งแรกครั้งเดียว) - โหลด package ด้วย
library()(ทำทุกครั้งที่เปิด session ใหม่)
# Install jsonlite
install.packages("jsonlite")
# Load jsonlite
library(jsonlite)
⬇️ Function 1. fromJSON()
fromJSON() ใช้เปลี่ยน JSON ที่เป็นอยู่ใน character (JSON string) ให้เป็น R object เช่น list และ data frame
เช่น เราสามารถเปลี่ยน JSON string แบบนี้:
# Create a JSON string
snacks_json_string <- '
[
{
"snack_name": "Corn Chips",
"brand": "Nibble & Dip",
"price_usd": 2.99,
"is_vegan": true
},
{
"snack_name": "Pita Chips",
"brand": "DipMates",
"price_usd": 4.25,
"is_vegan": false
},
{
"snack_name": "Tortilla Chips",
"brand": "Casa Crunch",
"price_usd": 3.49,
"is_vegan": true
}
]
'
ให้เป็น R object ได้แบบนี้:
# Convert to R object
snacks_r_obj <- fromJSON(snacks_json_string)
# View the df
snacks_r_obj
ผลลัพธ์:
snack_name brand price_usd is_vegan
1 Corn Chips Nibble & Dip 2.99 TRUE
2 Pita Chips DipMates 4.25 FALSE
3 Tortilla Chips Casa Crunch 3.49 TRUE
Note: fromJSON() จะเลือกประเภท R object ให้โดยอัตโนมัติ เช่น ถ้า JSON มีโครงสร้างคล้าย data frame (มีข้อมูลซ้ำ ๆ ที่ดูเหมือน rows และ columns) fromJSON() จะเปลี่ยน JSON string ให้เป็น data frame
⬆️ Function 2. toJSON()
toJSON() ใช้เปลี่ยน R object ให้เป็น JSON string
เช่น เปลี่ยน data frame นี้:
# Create a data frame
snacks_df <- data.frame(
snack_name = c("Seaweed Thins", "Chickpea Puffs", "BBQ Potato Crisps"),
brand = c("OceanBite", "LegumeLab", "Spud & Spark"),
price_usd = c(2.45, 3.35, 3.10),
is_vegan = c(TRUE, TRUE, FALSE),
stringsAsFactors = FALSE
)
ให้เป็น JSON string:
# Convert to JSON string
snacks_json_string <- toJSON(snacks_df)
# View the JSON string
snacks_json_string
ผลลัพธ์:
[{"snack_name":"Seaweed Thins","brand":"OceanBite","price_usd":2.45,"is_vegan":true},{"snack_name":"Chickpea Puffs","brand":"LegumeLab","price_usd":3.35,"is_vegan":true},{"snack_name":"BBQ Potato Crisps","brand":"Spud & Spark","price_usd":3.1,"is_vegan":false}]
สังเกตว่า ผลลัพธ์ที่ได้มาอ่านยาก เพราะข้อมูลอยู่ในบรรทัดเดียวกันหมด
ทั้งนี้ เราเปลี่ยนให้ผลลัพธ์อ่านง่ายขึ้นได้ โดยใช้ pretty argument แบบนี้:
# Convert to JSON string (pretty)snacks_json_string_pretty <- toJSON(snacks_df, pretty = TRUE)# View the JSON string (pretty)snacks_json_string_pretty
ผลลัพธ์:
[
{
"snack_name": "Seaweed Thins",
"brand": "OceanBite",
"price_usd": 2.45,
"is_vegan": true
},
{
"snack_name": "Chickpea Puffs",
"brand": "LegumeLab",
"price_usd": 3.35,
"is_vegan": true
},
{
"snack_name": "BBQ Potato Crisps",
"brand": "Spud & Spark",
"price_usd": 3.1,
"is_vegan": false
}
]
จะเห็นว่า ตอนนี้ JSON string อ่านง่ายกว่าก่อนหน้านี้มาก
📖 Function 3. read_json()
read_json() ใช้โหลดไฟล์ JSON เข้ามาใน R
เช่น เรามีไฟล์ชื่อ snacks.json ที่มีข้อมูลแบบนี้:
[
{
"snack_name": "Plantain Chips",
"brand": "TropiBite",
"price_usd": 3.15,
"is_vegan": true
},
{
"snack_name": "Rice Crackers",
"brand": "ZenMunch",
"price_usd": 2.25,
"is_vegan": true
},
{
"snack_name": "Pretzel Sticks",
"brand": "Twist & Dip",
"price_usd": 3.75,
"is_vegan": true
}
]
เราสามารถโหลดเข้ามาใน R ด้วย read_json() ได้แบบนี้:
# Read the JSON file
snacks_from_json_file <- read_json("snacks.json")
# View the result
snacks_from_json_file
ผลลัพธ์:
[[1]]
[[1]]$snack_name
[1] "Plantain Chips"
[[1]]$brand
[1] "TropiBite"
[[1]]$price_usd
[1] 3.15
[[1]]$is_vegan
[1] TRUE
[[2]]
[[2]]$snack_name
[1] "Rice Crackers"
[[2]]$brand
[1] "ZenMunch"
[[2]]$price_usd
[1] 2.25
[[2]]$is_vegan
[1] TRUE
[[3]]
[[3]]$snack_name
[1] "Pretzel Sticks"
[[3]]$brand
[1] "Twist & Dip"
[[3]]$price_usd
[1] 3.75
[[3]]$is_vegan
[1] TRUE
ทั้งนี้ read_json() มี simplifyVector argument ที่ช่วยโหลดข้อมูลที่เหมือน data frame ให้เป็น data frame (แทน list) ได้แบบนี้:
# Read the JSON file (simplified)
snacks_from_json_file_simplified <- read_json("snacks.json",
simplifyVector = TRUE)
# View the result (simplified)
snacks_from_json_file_simplified
ผลลัพธ์:
snack_name brand price_usd is_vegan
1 Plantain Chips TropiBite 3.15 TRUE
2 Rice Crackers ZenMunch 2.25 TRUE
3 Pretzel Sticks Twist & Dip 3.75 TRUE
✍️ Function 4. write_json()
write_json() ใช้เปลี่ยน R object ให้เป็นไฟล์ JSON
เช่น เปลี่ยน data frame นี้:
# Create a list
snacks_df <- data.frame(
snack_name = c("Cassava Chips", "Lentil Crisps", "Cheese Puffs"),
brand = c("RootRush", "Pulse & Crunch", "CheezyPop"),
price_usd = c(2.85, 2.65, 3.99),
is_vegan = c(TRUE, TRUE, FALSE),
stringsAsFactors = FALSE
)
ให้เป็นไฟล์ JSON แบบนี้:
# Write to JSON
write_json(snacks_df,
"snacks_df.json")
ผลลัพธ์:
[{"snack_name":"Cassava Chips","brand":"RootRush","price_usd":2.85,"is_vegan":true},{"snack_name":"Lentil Crisps","brand":"Pulse & Crunch","price_usd":2.65,"is_vegan":true},{"snack_name":"Cheese Puffs","brand":"CheezyPop","price_usd":3.99,"is_vegan":false}]
เช่นเดียวกับ toJSON(), write_json() มี pretty argument ที่ทำให้ผลลัพธ์อ่านง่ายขึ้นได้:
# Write to JSON (pretty)
write_json(snacks_df,
"snacks_df_pretty.json",
pretty = TRUE)
ผลลัพธ์:
[
{
"snack_name": "Cassava Chips",
"brand": "RootRush",
"price_usd": 2.85,
"is_vegan": true
},
{
"snack_name": "Lentil Crisps",
"brand": "Pulse & Crunch",
"price_usd": 2.65,
"is_vegan": true
},
{
"snack_name": "Cheese Puffs",
"brand": "CheezyPop",
"price_usd": 3.99,
"is_vegan": false
}
]
💪 Summary
ในบทความนี้ เราได้ไปทำความรู้จักกับ 4 functions จาก jsonlite ซึ่งเป็น package สำหรับทำงานกับ JSON ในภาษา R:
fromJSON(): JSON string → R objecttoJSON(): R object → JSON stringread_json(): ไฟล์ JSON → R objectwrite_json(): R object → JSON
😺 GitHub
ดูตัวอย่าง code และไฟล์ JSON ทั้งหมดในบทความนี้ได้ที่ GitHub
