Open In App

How to parse a JSON File in PHP?

Last Updated : 24 Jun, 2025
Suggest changes
Share
Like Article
Like
Report

Parsing JSON in PHP is simple and efficient. JSON (JavaScript Object Notation) is a lightweight format used to store data in key-value pairs. PHP offers built-in functions to handle JSON encoding and decoding, making it easy to work with JSON data.

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"
        }
    ]
}

In this example, Data It reads an array containing multiple objects, where each object contains key-value pairs.

Example: Student Details in JSON

{
    "Student": [
        {
            "Name": "Riya",
            "Roll": 7058,
            "subject": "java"
        },
        {
            "Name": "Anubha",
            "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.

Steps to Parse JSON in PHP

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": "Riya",
            "Roll": 7058,
            "subject": "java"
        },
        {
            "Name": "Anubha",
            "Roll": 7059,
            "subject": "SAP"
        }
    ]
}

2. Reading the JSON File Using file_get_contents()

In PHP, we use the file_get_contents() function to read the content of the JSON file. This function takes one parameter, which is the file path, and returns the content of the file as a string. This string can then be decoded and used in PHP.

Syntax:

file_get_contents('path/to/your/file.json');

Note:

  • file_get_contents() reads the content of a file into a string.
  • 'my_data.json' is the file name. It works if the file is in the same folder as the PHP script, or you need to specify the correct path if it's elsewhere.

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:

$data = json_decode($json, true);

Note: The second argument (true) ensures that the JSON data is returned as an associative array. Without it, the data is returned as an object.

Example: PHP Code to Parse JSON Data

Here is the complete PHP code that reads, the JSON file, then decodesoutputs it and output the result:

PHP
<?php 
$json = file_get_contents('my_data.json'); 

if ($json === false) {
    die('Error reading the JSON file');
}

$json_data = json_decode($json, true); 

if ($json_data === null) {
    die('Error decoding the JSON file');
}

echo "<pre>";
print_r($json_data);
echo "</pre>";

?>

Output
Warning: file_get_contents(my_data.json): failed to open stream: No such file or directory in /home/guest/sandbox/Solution.php on line 2
Error reading the JSON file

Output

Array
(
    [Student] => Array
        (
            [0] => Array
                (
                    [Name] => Riya
                    [Roll] => 7058
                    [subject] => java
                )
            [1] => Array
                (
                    [Name] => Anubha
                    [Roll] => 7059
                    [subject] => SAP
                )
        )
)

Conclusion

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.


Next Article

Similar Reads