Read and Parse JSON files in PHP

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

The function file_get_contents() will read the entire file into a string, while json_decode() will decode the JSON string.

Requirements:

  • PHP
  • PHP JSON extension – added and enabled by default since PHP 5.2.0

How To Read and Parse JSON Files in PHP.

The objective is to read and parse a JSON file and display it’s contents.

This tutorial will use the generated JSON file named members.json from the tutorial Create JSON Files In PHP.

Step 1.

Use file_get_contents() to read the contents of members.json into a string.

<?php

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

Step 2.

Use json_decode() to decode the JSON string of members.json.

<?php

$jsonFile_decoded = json_decode($jsonFile);

Step 3.

Now that the JSON string is decoded, different things can be done with the data, like displaying it by using print_r().

<?php

print_r($jsonFile_decoded);

All codes together.

<?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.

<?php

$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
                )

        )

)

Notes:

  • Since PHP 5.2.0, JSON extension was added and enabled by default.
  • As of PHP 8.0.0, the JSON extension is a core PHP extension, so it is always enabled.

References:


Posted

in

by

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *