How to Convert JSON to Array in Laravel 9 Example Tutorial

Reading Time: 4 minutes
1,733 Views

Inside this article we will see the concept i.e How to Convert JSON to Array in Laravel 9 Example Tutorial. Article contains the classified information about how to convert json data to array in laravel.

JSON is portable because parsers and writers are available for many, many languages. This means that JSON that a PHP script generates can be very easily understood by a JavaScript script.

If you are looking for a solution i.e JSON to PHP Array Converter in Laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1 – Convert JSON to Array (Using json_decode())

Open project into terminal and run this command to create controller file.

$ php artisan make:controller DemoController

It will create a file DemoController.php inside /app/Http/Controllers folder.

Open this controller file and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DemoController extends Controller
{
    public function index()
    {
        $jsonData = '[
                    { "id": 1, "name": "Sanjay", "email": "sanjay_sample@gmail.com"},
                    { "id": 2, "name": "Ashish", "email": "ashish_sample@gmail.com"},
                    { "id": 3, "name": "Vijay", "email": "vijay_sample@gmail.com"}
                ]';

        $data = json_decode($jsonData, true);

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

Route

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

//...
use App\Http\Controllers\DemoController;

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

//...

Output

Start development server & test.

URL: http://127.0.0.1:8000/data

Example #2 – Convert JSON to Array (Using Http::get())

Open DemoController.php file from /app/Http/Controllers folder. Write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class DemoController extends Controller
{
    public function index()
    {
        $response = Http::get('https://api.onlinewebtutorblog.com/employees');

        $data = $response->json();

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

Route

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

//...
use App\Http\Controllers\DemoController;

Route::get('apidata', [DemoController::class, "index"]);

//...

Output

Start development server & test.

URL: http://127.0.0.1:8000/apidata

We hope this article helped you to learn How to Convert JSON to Array in Laravel 9 Example 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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more