Parse JSON file with PHP

March 08, 2014 | Last tested: May 2015 | 957 views | Comments

Read and parse a JSON file using the functions file_get_contents() and json_decode() in PHP.

For example.

Parse a JSON file and display it's contents..

This tutorial will use the generated JSON file named members.json from the tutorial Create a JSON file in PHP.

$jsonFile = file_get_contents('members.json');
$jsonFile_decoded = json_decode($jsonFile);

print_r($jsonFile_decoded);

Result:

stdClass Object ( [members] => Array ( [0] => Array ( [0] => Jackson [1] => Barbara [2] => 27 [3] => F [4] => Florida ) [1] => Array ( [0] => Kimball [1] => Andrew [2] => 25 [3] => M [4] => Texas ) [2] => Array ( [0] => Baker [1] => John [2] => 28 [3] => M [4] => Arkansas ) [3] => Array ( [0] => Gamble [1] => Edward [2] => 29 [3] => M [4] => Virginia ) [4] => Array ( [0] => Anderson [1] => Kimberly [2] => 23 [3] => F [4] => Tennessee ) [5] => Array ( [0] => Houston [1] => Franchine [2] => 25 [3] => F [4] => Idaho ) [6] => Array ( [0] => Franklin [1] => Howard [2] => 24 [3] => M [4] => California ) [7] => Array ( [0] => Chen [1] => Dan [2] => 26 [3] => M [4] => Washington ) [8] => Array ( [0] => Daniel [1] => Carolyn [2] => 27 [3] => F [4] => North Carolina ) [9] => Array ( [0] => Englert [1] => Grant [2] => 25 [3] => M [4] => Delaware ) ) )

 

For debugging purposes and easy reading, add a header content-type before printing the decoded JSON data.

$jsonFile = file_get_contents('members.json');
$jsonFile_decoded = json_decode($jsonFile);

header('Content-Type: application/json');
print_r($jsonFile_decoded);

Result:

stdClass Object
(
    [members] => Array
        (
            [0] => Array
                (
                    [0] => Jackson
                    [1] => Barbara
                    [2] => 27
                    [3] => F
                    [4] => Florida
                )

            [1] => Array
                (
                    [0] => Kimball
                    [1] => Andrew
                    [2] => 25
                    [3] => M
                    [4] => Texas
                )

            [2] => Array
                (
                    [0] => Baker
                    [1] => John
                    [2] => 28
                    [3] => M
                    [4] => Arkansas
                )

            [3] => Array
                (
                    [0] => Gamble
                    [1] => Edward
                    [2] => 29
                    [3] => M
                    [4] => Virginia
                )

            [4] => Array
                (
                    [0] => Anderson
                    [1] => Kimberly
                    [2] => 23
                    [3] => F
                    [4] => Tennessee
                )

            [5] => Array
                (
                    [0] => Houston
                    [1] => Franchine
                    [2] => 25
                    [3] => F
                    [4] => Idaho
                )

            [6] => Array
                (
                    [0] => Franklin
                    [1] => Howard
                    [2] => 24
                    [3] => M
                    [4] => California
                )

            [7] => Array
                (
                    [0] => Chen
                    [1] => Dan
                    [2] => 26
                    [3] => M
                    [4] => Washington
                )

            [8] => Array
                (
                    [0] => Daniel
                    [1] => Carolyn
                    [2] => 27
                    [3] => F
                    [4] => North Carolina
                )

            [9] => Array
                (
                    [0] => Englert
                    [1] => Grant
                    [2] => 25
                    [3] => M
                    [4] => Delaware
                )

        )

)

 

Once the data has been parsed, many things can be done with it, like Inserting the records to a MySql Database, or Exporting the records to an Excel file.