How To Read JSON File in PHP Tutorial

Reading Time: 3 minutes
2,280 Views

Inside this article we will see the concept i.e How to read json file in PHP. Article contains classified information. It will give the complete idea of json file reading in PHP.

This tutorial will be super easy to understand and it’s steps are easier to implement in your code as well. If you learn reading json file here, you can use the same concept in data seeding to database via json file. This is step by step tutorial in PHP about json file reading.

How to convert a JSON into a PHP array, we will see the whole concept here in a very easy way.

Learn More –

Let’s get started.


Create an Application

Create a folder php-json in your localhost directory.

Inside this folder, create a file called index.php.

Suppose we have a JSON file as students.json

{
"students":[
	{
		"name": "Sanjay Kumar",
		"email": "sanjay@gmail.com"
	},
	{
		"name": "Ashish Kumar",
		"email": "ashish@gmail.com"
	},
	{
		"name": "Dhananjay Negi",
		"email": "dj@gmail.com"
	},
	{
		"name": "Vijay Rohila",
		"email": "vijay@gmail.com"
	}
 ]
}

Open index.php and write this complete code into it.

<?php

$students = json_decode(file_get_contents("students.json"), true);

echo "<pre>";
print_r($students);

Application Testing

Open browser and hit this URL: http://localhost/php-json

We hope this article helped you to learn How To Read JSON File in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.