How to Convert JSON to Array in Laravel
Oct 29, 2022, Originally published at techvblogs.com ・2 min read
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": "admin@techvblogs.com"},
{ "id": 2, "name": "Support", "email": "support@techvblogs.com"},
{ "id": 3, "name": "SslForWeb", "email": "admin@sslforweb.com"}
]';
$userData = json_decode($jsonData, true);
dd($userData);
}
}
Output:
array:3 [
0 => array:3 [
"id" => 1
"name" => "TechvBlogs"
"email" => "admin@techvblogs.com"
]
1 => array:3 [
"id" => 2
"name" => "Support"
"email" => "support@techvblogs.com"
]
2 => array:3 [
"id" => 3
"name" => "SslForWeb"
"email" => "admin@sslforweb.com"
]
]
{{ADD_HERE}}
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" => "atuny0@sohu.com"
"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.