Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Skip to content

 Blog
 

 About Us
 

 Disclaimer
 

 Privacy Policy
 

 Contact Us
 

 Promote with Online Web Tutor


 

 HTML Sitemap

 Home
 Tutorials
o
o
o
o
 Articles
o






o
o


 Premium Courses
o
o
o
o
o
o
o
o



o
o
o
o

o






o
o



o
o
o
o
o
o
o
o

How To Read JSON File in Laravel 8 Tutorial


January 6, 2022 by Sanjay Kumar
Table of Contents
 Laravel Installation
 JSON Data Preparation
 Create Controller
 Create Route
 Application Testing

Share this Article





Reading Time: 4 minutes
 14,487 Views
Inside this article we will see the concept i.e How to read json file in laravel 8. Article
contains classified information. It will give the complete idea of json file reading in
laravel 8.
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
laravel 8 about json file reading.

Learn More –

 How To Work with Laravel 8 Model Events Tutorial


 How To Work with Session Timeout in Laravel 8
 How to Work with Telescope In Laravel 8 Tutorial
 Jetstream Login Register Email Verification in Laravel 8
Let’s get started.

Laravel Installation
We will create laravel project using composer. So, please make sure your system
should have composer installed. If not, may be this article will help you to Install
composer in system.
Here is the command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

JSON Data Preparation


Let’s consider a .json file in application. We have a students.json inside /storage
folder.
{
"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"
}
]
}
You can place this .json file either in /storage folder or /public folder. We will read
only by specifying the path.
Create Controller
Open project into terminal and run this artisan command.

$ php artisan make:controller SiteController


It will create a file SiteController.php inside /app/Http/Controllers folder.
Assuming /storage folder.
00:03/03:27

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

SiteController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller


{
public function index()
{
$students = json_decode(file_get_contents(storage_path() . "/students.json"), true);

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

Concept

$students = json_decode(file_get_contents(storage_path() .
"/students.json"), true);
Here, we are parsing students.json file. storage_path() is a Laravel 8 helper
function which returns the path upto /storage folder.
If you have taken /public folder then you should use public_path() in place of it.
json_decode returns the data in object format. But putting true as second value it
returns the data into array format.
Now, we can insert the .json file data into database etc.

Create Route
Open web.php from /routes folder. Add this route into it.
web.php

//...

use App\Http\Controllers\SiteController;

Route::get("data", [SiteController::class, "index"]);


//...

Application Testing
Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1:8000/data

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

Buy Me a Coffee
Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4,
Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the
Coding Skills to Become an Expert in Web Development. So, Search your favourite
course and enroll now. Click here to join.
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.
Was this post helpful?
Let us know if you liked the post. Please share your feedback.
Yes
No
CategoriesLaravel 8Post navigation
How To Read CSV File in CodeIgniter 4 Tutorial
How To Read CSV File in Laravel 8 Tutorial
Newsletter

First name or full name


Email

Subscribe

report this ad
Advertisement

Popular Posts

 How to Get env Variable in Laravel 9 Blade Templates


 Laravel 9 Generate PDF with Chart Example Tutorial
 How To Create Countdown Timer Using Javascript Tutorial
 How To Convert Speech To Text In jQuery / Javascript
Find Us on Youtube
Online Web Tutor Youtube
Advertisement

Categories

 AWS S3 (4)
 Blogging (1)
 CakePHP 4 (101)
 CodeIgniter 4 (164)
 Guest Post (2)
 jQuery & Javascript (58)
 Laravel 8 (163)
 Laravel 9 (137)
 MySQL (10)
 Node Js (16)
 PHP Miscellaneous (80)
 Wordpress (28)
Recent Posts

 Laravel 9 Generate PDF with Chart Example Tutorial


 How to Get env Variable in Laravel 9 Blade Templates
 Laravel 9 Ajax PUT Request Example Tutorial
 Laravel 9 How To Return JSON Response Example Tutorial
 Laravel 9 How To Add Enumeration Column in Migration Tutorial

report this ad
© 2020 - 2022 Online Web Tutor

Learn Web Development Courses Risk Free @ $5 only.


Enroll Now
We use cookies to ensure that we give you the best experience on our website. If you continue to use this
site we will assume that you are happy with it.OkNoPrivacy policy
x
x

You might also like