How to Convert JSON to Array in Laravel - TechvBlogs

How to Convert JSON to Array in Laravel

In this article, You will learn How to Convert JSON to Array in Laravel.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

1 year ago

TechvBlogs - Google News

This article will teach us how to convert JSON to Array in laravel. I will explain through a simple example converting a JSON object to an Array in laravel.

There are many ways to convert JSON data into an array in a Laravel application. I'll show you how to convert JSON data into an array in 2 easy examples. In the first example, we will use json_decode(), and in the second example, we will use the json() method of the HTTP response. So let's see both examples now:

Using json_decode() Method

App\Http\Controllers\UserContoller.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
 
class UserController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $jsonUserData = '[
           { "id": 1, "name": "TechvBlogs", "email": "[email protected]"},
           { "id": 2, "name": "Support", "email": "[email protected]"},
           { "id": 3, "name": "SslForWeb", "email": "[email protected]"}
        ]';
 
        $userData = json_decode($jsonData, true);
  
        dd($userData);
    }
}

Output:

array:3 [
  0 => array:3 [
    "id" => 1
    "name" => "TechvBlogs"
    "email" => "[email protected]"
  ]
  1 => array:3 [
    "id" => 2
    "name" => "Support"
    "email" => "[email protected]"
  ]
  2 => array:3 [
    "id" => 3
    "name" => "SslForWeb"
    "email" => "[email protected]"
  ]
]

Using json() Method

App\Http\Controllers\UserContoller.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Http;
 
class UserController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $response = Http::get('https://dummyjson.com/users/1');
        $userData = $response->json();
   
        dd($userData);
    }
}

Output:

array:27 [
  "id" => 1
  "firstName" => "Terry"
  "lastName" => "Medhurst"
  "maidenName" => "Smitham"
  "age" => 50
  "gender" => "male"
  "email" => "[email protected]"
  "phone" => "+63 791 675 8914"
  "username" => "atuny0"
  "password" => "9uQFF1Lh"
  "birthDate" => "2000-12-25"
  "image" => "https://robohash.org/hicveldicta.png"
  "bloodGroup" => "A−"
  "height" => 189
  "weight" => 75.4
  "eyeColor" => "Green"
  "hair" => array:2 [
    "color" => "Black"
    "type" => "Strands"
  ]
  "domain" => "slashdot.org"
  "ip" => "117.29.86.254"
  "address" => array:5 [
    "address" => "1745 T Street Southeast"
    "city" => "Washington"
    "coordinates" => array:2 [
      "lat" => 38.867033
      "lng" => -76.979235
    ]
    "postalCode" => "20020"
    "state" => "DC"
  ]
  "macAddress" => "13:69:BA:56:A3:74"
  "university" => "Capitol University"
  "bank" => array:5 [
    "cardExpire" => "06/22"
    "cardNumber" => "50380955204220685"
    "cardType" => "maestro"
    "currency" => "Peso"
    "iban" => "NO17 0695 2754 967"
  ]
  "company" => array:4 [
    "address" => array:5 [
      "address" => "629 Debbie Drive"
      "city" => "Nashville"
      "coordinates" => array:2 [
        "lat" => 36.208114
        "lng" => -86.586212
      ]
      "postalCode" => "37076"
      "state" => "TN"
    ]
    "department" => "Marketing"
    "name" => "Blanda-O'Keefe"
    "title" => "Help Desk Operator"
  ]
  "ein" => "20-9487066"
  "ssn" => "661-64-2976"
  "userAgent" => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/12.0.702.0 Safari/534.24"
]

Thank you for reading this article.

Read Also: How to get the current URL in Vue JS

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.

Comments (0)

Comment


Note: All Input Fields are required.