Parse JSON file with PHP

Posted on 29th February 2016

JSON (Java Script Object Notation) is fast becoming the most common data format for asynchronous browser-server communication. Standard functions are available in PHP for generating, parsing and interpreting JSON format. The PHP program in this how-to guide reads a JSON file into a JSON string and uses json_decode() function to convert the JSON string into a PHP variable. JSON data consists of nested key-value pairs.

Consider a JSON file students.json containing student names, age and address as below.

[
 { "name":"Adam", "Age":18,
 	"Address": { "Street":"1000 Brooklyn Meadow", "City":"Denver" } },
 { "name":"Bob", "Age":30,
 	"Address": { "Street":"23 Sharp Crescent", "City":"Bristol"} },
 { "name":"Kaneko", "Age":27, 
 	"Address":{"Street":"7 Yuki Street","City":"Tokyo"} }
]

The following PHP program calls the file_get_contents() to read JSON from student_data.json file and parse this JSON file into a nested array using json_decode() function.

<?php

// Read JSON file
$json = file_get_contents('./student_data.json');

//Decode JSON
$json_data = json_decode($json,true);

//Print data
print_r($json_data);

?>

Output will be similar to:

Array
(
   [0] => Array
     (
       [name] => Adam
       [Age] => 18
       [Address] => Array
           (
             [Street] => 1000 Brooklyn Meadow
             [City] => Denver
            )

     )

   [1] => Array
     (
       [name] => Bob
       [Age] => 30
       [Address] => Array
           (
             [Street] => 23 Sharp Crescent
             [City] => Bristol
           )

     )

   [2] => Array
     (
       [name] => Kaneko
       [Age] => 27
       [Address] => Array
           (
             [Street] => 7 Yuki Street
             [City] => Tokyo
           )

     )

)

The json_decode() converts the JSON data to a nested array. The first argument of json_decode function is the string to decode. The second argument can be set to either true or false. If set to true, json_decode will return an associative array which has string indexes instead of numeric indexes.

THe json_decode function return NULL if the JSON string cannot be decoded. In which case you can call the json_last_error_msg() function to check the error message.

You can traverse a JSON object using foreach loops, using recursion or using iterators.

The following code sample prints out the students whose age is less than 20.


<?php

// Read JSON file
$json = file_get_contents('./student_data.json');

//Decode JSON
$json_data = json_decode($json,true);

//Traverse array and get the data for students aged less than 20
foreach ($json_data as $key1 => $value1) {
    if($json_data[$key1]["Age"] < 20){
        print_r($json_data[$key1]);
    }
}
?>

For more details on getting data from JSON objects, read Getting data from a nested JSON in PHP using Recursion and Loops


Post a comment

Comments

GOUTHAM REDDY | June 5, 2018 10:33 AM |

How to echo it ?

Gary | June 5, 2018 2:59 PM |

you need to traverse the key-value pair in a loop and call the print_r function