John Davidson

PHP unable to decode nested response from database

0 comments
Message:


So I'am trying to decode the response iam getting. The current response iam now receiving is as follows;


[ { "RESPONSE": "{\"Result\":[{\"ERRORS\":\"99012: Weborder number already exists : 20211049\"}]}" }, { "RESPONSE": "{\"Result\":[{\"ERRORS\":\"99012: Weborder number already exists : 20211048\"}]}" }, { "RESPONSE": "{\"Result\":[{\"ERRORS\":\"99012: Weborder number already exists : 20211050\"}]}" }, { "RESPONSE": "{\"Result\":[{\"ERRORS\":\"99012: Weborder number already exists : 20211050\"}]}" }, { "RESPONSE": "{\"Result\":[{\"ERRORS\":\"99012: Weborder number already exists : 20211049\"}]}" }, { "RESPONSE": "{\"Result\":[{\"ERRORS\":\"99012: Weborder number already exists : 20211046\"}]}" }, 

This is how iam trying to get the decoded code:


    public function index(Request $request)
{
//
// $log = Log::get('RESPONSE');
// Get all the Response rows from database.
$data = Log::get('RESPONSE');

// Convert data into json array
//$decoded = json_encode($data, true);
// dd($decoded);
//$decoded = json_encode($data, JSON_UNESCAPED_SLASHES);
//$test_decoded = json_decode($decoded, true);
//$test = json_decode($decoded, true);
// foreach($decoded['RESPONSE'] as $response){
// $response['Result'];
// //json_decode($response);
// //if ($response['Result'])
// }
//dd($decoded);
//return($decoded);
//$again_decode = json_decode($decoded);
//$response_key_value = $decoded['RESPONSE'];

//dd($test);

$decoded = json_decode($data);

return ($decoded);
}


I have left all the commented code in so you will be able to see what i already have tried. What iam looking to achieve is the following:


 { "RESPONSE": 
"{"Result":
{"ERRORS":"99012: Weborder number already exists : 20211049"}

I think iam stuck with a nested json query and within that nested json query i have an array of one json query. is it possible to extract the last json query. any help would be greatly appreciated.



My Answer:
"id": 1,
"name": "John Doe",
"email": "johndoe@example.com",
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
}
} ]

To decode this nested response in PHP, you can use the json_decode function with the second parameter set to true to convert the JSON string into an associative array. Here's an example code snippet to decode the response:

php
$response = '[ { "id": 1, "name": "John Doe", "email": "johndoe@example.com", "address": { "street": "123 Main St", "city": "New York", "state": "NY", "zip": "10001" } } ]';

$data = json_decode($response, true);

foreach ($data as $item) {
echo "ID: " . $item['id'] . "
";
echo "Name: " . $item['name'] . "
";
echo "Email: " . $item['email'] . "
";

$address = $item['address'];
echo "Address: " . $address['street'] . ", " . $address['city'] . ", " . $address['state'] . " " . $address['zip'] . "
";
}


This code will decode the nested response and print out the ID, name, email, and address information for each item in the response array.

Rate this post

5 of 5 based on 1570 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us