How to Convert JSON to Array in Laravel
In this post we will give you information about How to Convert JSON to Array in Laravel. Hear we will give you detail about How to Convert JSON to Array in Laravel And how to use it also give you demo for it if it is necessary.
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
AppHttpControllersUserContoller.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$jsonUserData = '[
{ "id": 1, "name": "onlinecode", "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" => "onlinecode"
"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
AppHttpControllersUserContoller.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHttp;
use IlluminateSupportFacadesHttp;
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.
Also see: How to get the current URL in Vue JS
. .
If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.
Hope this code and post will helped you for implement How to Convert JSON to Array in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs