B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP PHP JSON Tutorial | LiveCodeProgramming

PHP JSON Tutorial

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is widely used to transmit data between a server and web application. In PHP, we work with JSON using json_encode() and json_decode().

JSON (JavaScript Object Notation) एक हल्का डेटा-इंटरचेंज फॉर्मेट है। इसका उपयोग सर्वर और वेब एप्लिकेशन के बीच डेटा भेजने के लिए किया जाता है। PHP में json_encode() और json_decode() फंक्शन का उपयोग करके JSON के साथ काम किया जाता है।

Encoding PHP Arrays to JSON

<?php
$data = ["name" => "Aryan", "age" => 16];
echo json_encode($data);
?>
{"name":"Aryan","age":16}

Decoding JSON to PHP

<?php
$json = '{"name":"Aryan","age":16}';
$data = json_decode($json, true);
print_r($data);
?>
Array ( [name] => Aryan [age] => 16 )

Check for JSON Errors

<?php
$badJson = '{"name":"Aryan"';
json_decode($badJson);
if (json_last_error() !== JSON_ERROR_NONE) {
    echo "JSON Error: " . json_last_error_msg();
}
?>
JSON Error: Syntax error