สามารถใช้ฟังก์ชัน json_decode เพื่อแปลงข้อมูล JSON ให้กลายเป็น Array ใน PHP ได้ดังนี้
json_decode($json_string_that_needs_to_be_converted, true);
พารามิเตอร์ที่สอง true จะบังคับให้คืนค่าเป็น Associative Array แทน Object
ตัวอย่างการใช้งาน
$json_string = '["m@gmail.com","p@gmail.com","q@gmail.com"]';
$array_of_data = json_decode($json_string, true);
รูปแบบอีกวิธีหนึ่ง คือการกำหนด JSON String ด้วยเครื่องหมายคำพูดคู่ (Double quotes) และ Escape ตัวอักขระภายใน
$json_string = "[\\"m@gmail.com\\",\\"p@gmail.com\\",\\"q@gmail.com\\"]";
$array_of_data = json_decode($json_string, true);
ผลลัพธ์ที่ได้
Array
(
[0] => m@gmail.com
[1] => p@gmail.com
[2] => q@gmail.com
)
หมายเหตุ: JSONL (JSON Lines) คือรูปแบบข้อมูลที่แต่ละบรรทัดเป็น JSON Object 完整อันหนึ่ง หากต้องการแปลง JSONL ทั้งไฟล์ ให้อ่านทีละบรรทัดแล้ว decode แต่ละบรรทัดแยกกัน