Explain PHP JSON
PHP provides built-in functions to work with JSON (JavaScript Object Notation), a popular data interchange format. These functions are json_encode
and json_decode
.
Encoding JSON
json_encode
converts a PHP variable (such as an array or object) into a JSON string.
Example 1: Encoding an array to JSON
<?php
$data = array(
"name" => "John Doe",
"age" => 30,
"city" => "New York"
);
$json = json_encode($data);
echo $json; // Output: {"name":"John Doe","age":30,"city":"New York"}
?>
Example 2: Encoding a multi-dimensional array
<?php
$data = array(
"employees" => array(
array("name" => "John Doe", "age" => 30, "city" => "New York"),
array("name" => "Jane Doe", "age" => 25, "city" => "Los Angeles")
),
"company" => "Tech Solutions"
);
$json = json_encode($data, JSON_PRETTY_PRINT);
echo $json;
/*
Output:
{
"employees": [
{
"name": "John Doe",
"age": 30,
"city": "New York"
},
{
"name": "Jane Doe",
"age": 25,
"city": "Los Angeles"
}
],
"company": "Tech Solutions"
}
*/
?>
Decoding JSON
json_decode
converts a JSON string into a PHP variable.
Example 1: Decoding a JSON string to an associative array
<?php
$json = '{"name":"John Doe","age":30,"city":"New York"}';
$data = json_decode($json, true);
print_r($data);
/*
Output:
Array
(
[name] => John Doe
[age] => 30
[city] => New York
)
*/
?>
Example 2: Decoding a JSON string to an object
<?php
$json = '{"name":"John Doe","age":30,"city":"New York"}';
$data = json_decode($json);
echo $data->name; // Output: John Doe
echo $data->age; // Output: 30
echo $data->city; // Output: New York
?>
Example 3: Handling complex JSON structure
<?php
$json = '{
"employees": [
{"name": "John Doe", "age": 30, "city": "New York"},
{"name": "Jane Doe", "age": 25, "city": "Los Angeles"}
],
"company": "Tech Solutions"
}';
$data = json_decode($json, true);
print_r($data);
/*
Output:
Array
(
[employees] => Array
(
[0] => Array
(
[name] => John Doe
[age] => 30
[city] => New York
)
[1] => Array
(
[name] => Jane Doe
[age] => 25
[city] => Los Angeles
)
)
[company] => Tech Solutions
)
*/
?>
Handling JSON Errors
PHP provides json_last_error
and json_last_error_msg
functions to check for errors during JSON encoding or decoding.
Example: Checking for JSON errors
<?php
$json = '{"name":"John Doe", "age":30, "city":"New York"'; // Missing closing }
$data = json_decode($json);
if (json_last_error() != JSON_ERROR_NONE) {
echo "JSON Error: " . json_last_error_msg(); // Output: JSON Error: Syntax error
}
?>
These are the basic operations you can perform with JSON in PHP. Using json_encode
and json_decode
, you can easily convert between PHP data structures and JSON format for data interchange.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.