How to parse a JSON File in PHP?
We will explore how to parse a JSON file and display its data using PHP. PHP is a server-side scripting language commonly used to process and manipulate data. JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for both humans and machines to read and write. It stores data as name/value pairs.
JSON Syntax Overview
The general syntax of a JSON object looks like this:
{
"Data": [
{
"key1": "value1",
"key2": 123,
"key3": "value3"
},
{
"key1": "value4",
"key2": 456,
"key3": "value6"
}
]
}
Here, Data is an array containing multiple objects, where each object contains key-value pairs.
Example: JSON Notation for Student Details
{
"Student": [
{
"Name": "Sravan",
"Roll": 7058,
"subject": "java"
},
{
"Name": "Jyothika",
"Roll": 7059,
"subject": "SAP"
}
]
}
Advantages of JSON
- No End Tags: Unlike XML, JSON doesn’t require closing tags.
- Compact: JSON uses a shorter and more concise format compared to XML.
- Faster: JSON is quicker to read and write, making it ideal for data exchange.
- Supports Arrays: JSON can handle arrays, making it flexible for storing structured data.
Approach: Parsing JSON in PHP
Let’s parse the JSON file in PHP and display the data. We will take a JSON file that contains student details and then load and decode it using PHP functions.
Step 1: Create a JSON File
Create a JSON file named my_data.json. For this example, the file contains student data in the following format:
{
"Student": [
{
"Name": "Sravan",
"Roll": 7058,
"subject": "java"
},
{
"Name": "Jyothika",
"Roll": 7059,
"subject": "SAP"
}
]
}
Step 2: Reading the JSON File Using file_get_contents()
In PHP, we use the file_get_contents() function to read the JSON file. This function reads the content of the file into a string, which can then be decoded and used in PHP.
Syntax:
file_get_contents(path, file_name)
- file_name is the name of the file and path is the location to be checked.
- Use json_decode() function to decode to JSON file into array to display it.
Step 3: Decoding JSON Data Using json_decode()
The json_decode() function is used to convert the JSON string into a PHP array or object. By passing true as the second argument, the function converts the JSON data into an associative array.
Syntax:
json_decode($json_object, true)
$json_object is the file object to be read.
Example: PHP Code to Parse JSON Data
Below is a PHP script that reads and parses the JSON data from the my_data.json file:
<?php
// Read the JSON file
$json = file_get_contents('my_data.json');
// Check if the file was read successfully
if ($json === false) {
die('Error reading the JSON file');
}
// Decode the JSON file
$json_data = json_decode($json, true);
// Check if the JSON was decoded successfully
if ($json_data === null) {
die('Error decoding the JSON file');
}
// Display data
echo "<pre>";
print_r($json_data);
echo "</pre>";
?>
Output:
Array (
[Student] => Array (
[0] => Array (
[Name] => Sravan
[Roll] => 7058
[subject] => java
)
[1] => Array (
[Name] => Jyothika
[Roll] => 7059
[subject] => SAP
)
)
)
Parsing JSON data in PHP is simple and efficient. With the use of functions like file_get_contents() and json_decode(), you can easily load and process JSON data in your PHP applications. JSON’s lightweight structure and ease of use make it a popular choice for data exchange between server and client-side scripts.