JSON data can be accessed and utilized with many programming languages. In this article, we will learn how to read a JSON file in PHP. We will discuss all the methods used to read a JSON file, convert JSON objects into an array, and display it in PHP. Let’s first learn about what JSON is.
Read a JSON file with a PHP code example
<?php
// Reads the JSON file.
$json = file_get_contents('./test_data.json');
// Decodes the JSON file.
$json_data = json_decode($json,true);
// Display the JSON data.
print_r($json_data);
?>

Table of Contents
- What is JSON?
- Encode JSON Data in PHP.
- Decode JSON Data in PHP.
- Read a JSON File using the file_get_contents() Function in PHP.
- Read JSON Files with PHP Code Examples Explored.
What is JSON?
JSON stands for JavaScript object notation. The JSON data is written as name/value pairs, and it is basically a lightweight data-interchange format that is very easy to parse and generate.
JSON is based on two basic structures:
- Object: It is a collection of key/value pairs (i.e., key: value). And each object begins with a left curly bracket { and then ends with a right curly bracket }. And the multiple key/value pairs are separated by a (comma,).
- Array: An ordered list of values starts with a left bracket [ and ends with a right bracket ]. The values in an array are separated by a (comma,).
In JSON data, the keys are always strings, but the value can be a string, number, true or false, null, or even an object or an array. Strings must be enclosed in double quotes ” and can contain escape characters such as \n, \t, and \.
A JSON object may look like this:
{
"Profile": {
"name": "John",
"Last Name": "Stones",
"Born": 1995,
"Gender": "Male"
}
}
An example of JSON array:
{
"Colors": [
"Red",
"Green",
"Orange",
"Black",
"White"
]
}
Encode JSON Data in PHP
In PHP, the json_encode() function encodes a PHP data value in JSON format. The value being encoded can be any PHP data type except a resource, like a database or a file handle. The below example demonstrates how to encode a PHP associative array into a JSON object.
<?php
// An associative array
$age = array("Peter"=>20, "John"=>80, "Mark"=>78, "Clark"=>90);
echo json_encode($age);
?>
Output:
{"Peter":20,"John":80,"Mark":78,"Clark":90}
We can also force the json_encode() function to return a PHP indexed array as a JSON object by using the JSON_FORCE_OBJECT option, as shown in the example below:
<?php
// An indexed array
$phones = array("Iphone", "Samsung", "Nokia", "Sony");
echo json_encode($phones, JSON_FORCE_OBJECT);
?>
Output:
{"0":"Iphone","1":"Samsung","2":"Nokia","3":"Sony"}
As we can see in the above example, we encoded a PHP array into a JSON object.
Decode JSON Data in PHP
In PHP, the json_decode() function converts the JSON encoded string into the appropriate PHP data type. The following example demonstrates how to decode a JSON object to a PHP object.
<?php
// Storing the JSON data in a PHP variable
$json = '{"Mark":40,"Henry":50,"John":60,"Joel":35}';
// Displaying the result.
var_dump(json_decode($json));
?>
Output:
object(stdClass)#1 (4) {
["Peter"]=>
int(65)
["Harry"]=>
int(80)
["John"]=>
int(78)
["Clark"]=>
int(90)
}
The json_decode() function returns an object in PHP by default. But we can also optionally specify a second parameter, $assoc, which accepts a boolean value. When we set the value as true, the JSON objects are decoded into associative arrays.
Here’s an example:
<?php
// Storing the JSON data in a PHP variable
$json = '{"Mark":40,"Henry":50,"John":60,"Joel":35}';
// Displaying the result.
var_dump(json_decode($json, true));
?>
Output:
array(4) {
["Mark"]=>
int(40)
["Henry"]=>
int(50)
["John"]=>
int(60)
["Joel"]=>
int(35)
}
Read a JSON File using file_get_contents() Function in PHP
PHP has a built-in function called file_get_contents(). It is used to read a JSON file in PHP. PHP also provides a json_encode() function to encode and a json_decode() function to decode JSON data. We will use these functions to read a JSON file in PHP in the following example.
First, create a JSON file and save it as test_data.JSON. In our example, we have taken some student data in JSON.
JSON File Data
{
"Student":[{
"Name":"Mark",
"Roll No":101,
"subject":"PHP"
},
{
"Name":"John",
"Roll":202,
"subject":"Python"
},
{
"Name":"Silva",
"Roll":301,
"subject":"JavaScript"
}
]
}
Now, we will use the file_get_contents() function to read a JSON file into PHP.
Syntax:
file_get_contents(path, file_name)
We will also use json_decode() to decode a JSON file into an array and display it.
Syntax:
json_decode($json_object, true)
The following code demonstrates how to read a JSON file in PHP.
<?php
// Reads the JSON file.
$json = file_get_contents('./test_data.json');
// Decodes the JSON file.
$json_data = json_decode($json,true);
// Display the JSON data.
print_r($json_data);
?>
Output:
Array
(
[Student] => Array
(
[0] => Array
(
[Name] => Mark
[Roll No] => 101
[subject] => PHP
)
[1] => Array
(
[Name] => John
[Roll] => 202
[subject] => Python
)
[2] => Array
(
[Name] => Silva
[Roll] => 301
[subject] => JavaScript
)
)
)
We used PHP’s file_get_contents() function in the above code example to read the JSON file. We also use the json_decode() function, which converts the JSON file to a nested array. In the output, as you can see, it displays student data in an array format.
Read JSON Files with PHP Code Examples Explored
We can read a JSON file using PHP’s file_get_contents() function. We can use the json_decode() function to convert JSON data in a nested array and then display it.
This article explains reading a JSON file using PHP’s file_get_contents() function. It also describes what JSON is and its basic structure.
This article also focuses on how to encode and decode JSON data in PHP, and we hope you find this article helpful.
Dig deeper and learn more about JSON in PHP
- Recursive Loop Multidimensional JSON Arrays in PHP
- How to convert JSON object to array in PHP
- How to Convert XML to JSON in PHP
- How to Convert JSON to XML with PHP
- How to merge two JSON objects in PHP
- PHP serialize vs json_encode
Learn the Fundamentals of Good Web Development.
Take a moment and sign up for our free email course on the fundamentals of good web development. Every week is packed with a roundup of articles on our site and from around the web, where we go deep into developing exceptional web applications. We have meetups, code reviews, slack chats, and more.