Able Alias Able Alias - 7 months ago 19
PHP Question

php response body as object array, how to parse the content

one of the API return response as object array, I json encode the object array as below

{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"MISSING_REQUIRED_PARAMETER","detail":"Missing required parameter.","field":"amount_money.amount"}]}


I want to get the detail field value, how to get it.

Answer

You can read detail field right away from response array.

<?php
$json = '{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"MISSING_REQUIRED_PARAMETER","detail":"Missing required parameter.","field":"amount_money.amount"}]}';

I'm converting your JSON to Array just for example.

//Actual Array Response
$a = json_decode($json, true);

echo "<pre>";
print_r($a);
echo "</pre>";

//Save detail to Variable
$detail = $a['errors'][0]['detail'];
echo $detail;
?>