'PHP unable to decode nested response from database

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.

php


Solution 1:[1]

The value of your RESPONSE node is a string (hence the escaped \" in the value), so you need to json_decode() it too:

$decoded = \json_decode($data);
$responseDecoded = \json_decode($decoded['RESPONSE']);
\var_dump($responseDecoded);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin Orlowski