Black User Black User - 7 months ago 31
JSON Question

Extracting Json Data from PHP log file and Adding in a new column

I am newbie to php,
Here is my json file ,

{"hint_data":{"locations":["AXQDAP____8AAAAABwAAABEAAAAYAAAAIwIAAERwAgAAAAAADgyCAef7TAMCAAEB","bOsDAP____8AAAAAAwAAAAcAAADFAQAAFAAAAEJwAgAAAAAANQeCAdzdTAMFAAEB"],"checksum":326195011},"route_name":["",""],"via_indices":[0,15],"via_points":[[25.299982,55.376873],[25.29874,55.369179]],"found_alternative":false,"route_summary":{"end_point":"","start_point":"","total_time":101,"total_distance":871},"route_geometry":"{_ego@m}|rhBpBaBvHuC`EuArEUtEtAlDvEnD`MlDvMli@hsEfFzn@QlTgNhwCs@fKwBjF","status_message":"Found route between points","status":0}

I need to extract the total_time only from the json and print the extracted total_time to a new column in a csv file,could someone please help me in that?


Save your json string into a json file, load it and you can use json_decode function to load it in PHP as an array.

$string = file_get_contents('file.json');
$json = json_decode($string, true);
$totalTime = $json['route_summary']['total_time']; // 101

How to export the results to CSV file afterwards you have examples here on SO.