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 a 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
}
?>
Conclusion
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.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
