printing the json result with php

irandoct

New member
Hi,
I want to get and print "trackingCode" from the following json. how to do that?

{"status":200,"family":"SUCCESSFUL","reason":"OK","data":"{\"status\":200,\"family\":\"SUCCESSFUL\",\"reason\":\"OK\",\"data\":{\"status\":200,\"family\":\"SUCCESSFUL\",\"reason\":\"OK\",\"data\":{\"result\":{\"trackingCode\":27009,\"error_Msg\":null,\"error_Code\":null,\"complemantary_Msg\":\"saved successfully!\",\"head_EPRSC_ID\":\"4065959687\"}}}}"}


Thanks
 
Well, you need below code for what you are looking for.

$jsonString = '{"status":200,"family":"SUCCESSFUL","reason":"OK","data":"{\"status\":200,\"family\":\"SUCCESSFUL\",\"reason\":\"OK\",\"data\":{\"status\":200,\"family\":\"SUCCESSFUL\",\"reason\":\"OK\",\"data\":{\"result\":{\"trackingCode\":27009,\"error_Msg\":null,\"error_Code\":null,\"complemantary_Msg\":\"saved successfully!\",\"head_EPRSC_ID\":\"4065959687\"}}}}"}';

// Decode the JSON string
$data = json_decode($jsonString, true);

// Navigate through the nested structure to get the trackingCode
$trackingCode = $data['data']['data']['data']['result']['trackingCode'];

// Print the trackingCode
echo "Tracking Code: " . $trackingCode;

Thanks
 
Back
Top