0

I need to extract the Object id total_time from this file containing multiple json objects

{"alternative_names":[["",""]],"route_name":["",""],"status_message":"Found route between points","route_geometry":"qo{`o@wpslhBmFZwEpBgDzDeBlFWdGv@`G`CzEyBfIyEtEoNtEuMpAuHkAsCgC[mB{AgCuA{@uCa@}Cl@yBtB}@lDVhEcAtDmCdBcDx@iDDeDk@uBeBeB_CcGcXyFi^{Dg\\wBkP_Fs^wBiS_Ce[_D}UwAcKiBqIgI_TeLaUcMmP_IsHqf@_WyJeE}F`MkDjIiTth@y|@loBq^vy@mu@fw@cUxOco@r^gb@zRoOjPsBfMq@rMjBdPnEpKlEnH`J~Dvn@la@nd@pT|f@lLv_@pRzLbHnb@fUxc@lMnaC~zAncBxnA`kB|bB`|CbmCl]rY`xCzkCjwBzoBjxGl_G|fNhcMf~E~mE~pFt}EjbBlzAhvInyHfqDb~CblA`w@poAzt@z}@bb@diD~dBxtEjgCfjDljBpuAfw@bpGngDhqF~yC~g@la@v~@ni@rbAjg@jP`JlC`FNdDGdDu@j@eAhBa@lCLdCaCjI_BtCy@vA_DvF}qAf}Bof@l{@{CnFoKdR}mA`xBgM|TkJxPmLlSsf@|}@gf@`x@kOzXmEfGmSx^kf@tw@mDtL}k@r~@ykCjjEau@niAee@bu@uUl\\}DpFzCrCr|@dt@|NbLroBx_BdZlV|DbD`pBr~Anq@xm@r|@ls@|y@dq@}O`XuM`QcDhEoBgBeCCu[xXmBH","route_summary":{"end_point":"","start_point":"","total_time":824,"total_distance":15391},"found_alternative":true,"alternative_summaries":[{"end_point":"","start_point":"","total_time":886,"total_distance":14967}],"via_points":[[25.196808,55.273754],[25.139168,55.187702]],"status":0,"via_indices":[0,144],"hint_data":{"locations":["TdMLAP____8AAAAADwAAAA8AAAA9AAAAbAAAAOtwBgAAAAAACXmAARxpSwMEAAEB","4Q4AAGKyBgAAAAAACQAAAAAAAAAhAQAAAAAAAGUYAAAAAAAA4Zd_AfgYSgMGAAEB"],"checksum":326195011},"alternative_geometries":["qo{`o@wpslhBmFZwEpBgDzDeBlFWdGv@`G`CzEyBfIyEtEoNtEuMpAuHkAsCgC[mB{AgCuA{@uCa@}Cl@yBtB}@lDVhEcAtDmCdBcDx@iDDeDk@uBeBeB_CcGcXyFi^{Dg\\wBkP_Fs^wBiS_Ce[_D}UwAcKiBqIgI_TeLaUcMmP_IsHqf@_WyJeE}F`MkDjIiTth@y|@loBq^vy@ec@vfAyeAfcCok@vtA}Wtx@uS|d@skAvfCgExJgJpTqH|O}`@n~@wSve@qEhKkYdp@gB~DuUrk@_JdRsAhDmJlVqd@rcAuM|Zeg@zkAiKzUgIbS}B~EtDvBhr@`a@dQ~J|rBpmAj_CfyAlAzD^lDgHnOyQlg@w[l|@}Q~d@eBB}Af@mAdAw@bBWpBJrBn@hBaAjGif@ptAk^rmAoVxy@cHjPmC`H`E|Bp[xRpc@`ZfLvIzLlIreG~cEjrArz@rmAl|@lbDrzBjWlQ|xA~bAho@pc@d_@jWng@j^hNzJvGnEnPjLlAx@lgAhv@bGvCjkBvoAbjDr}BvfAx{@t\\rSfoDviC~zAjgAfOxIvAbAjwCtsBv|@|m@bD|BvL`Id_Dt|BtLlIxsAx_AfE~Crn@hc@rKvIh_BniApe@z\\xIpGtoBpxAtEdDrQrMzjChlB`~@lo@nwF`pE|jBl|AzFzE|DfDvDrCjKfJr`@n\\pLzK`nBzzAdO~LhaFxaEzCdCnLpKbp@ti@nIjHdRzMjo@tg@~MvKpwCtdClw@bm@z[bWfr@vk@zCrCr|@dt@|NbLroBx_BdZlV|DbD`pBr~Anq@xm@r|@ls@|y@dq@}O`XuM`QcDhEoBgBeCCu[xXmBH"],"alternative_indices":[0,183]}

{"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}

And I am Using this Script,

 <?php

$data = file_get_contents('sample.json');
$data = json_decode($data);
$totalTime  = $data->route_summary->total_time;
var_dump($totalTime);
?>

But It gives me a NULL Output.Any help,would be appreciated?

3
  • Do you know anything about the structure of that file? Is not a valid json, so you can't do that straightforward. You may have to divide it first. Commented May 28, 2016 at 11:21
  • 1
    $data = json_decode($data,true); Commented May 28, 2016 at 11:21
  • @jothi no it's not working Commented May 28, 2016 at 12:25

2 Answers 2

1

As you have mentioned, there are two json objects, then i suggest to read the file per line. Then for each line you can decode json or dump it. Hope that helps.

$lines = file('json.data');

foreach ($lines as $line_num => $line) {
   // you are looking for first object
  $data = json_decode($line);
  $totalTime  = $data->route_summary->total_time;
  var_dump($totalTime);
 break;

}
Sign up to request clarification or add additional context in comments.

3 Comments

I get error ErrorException (E_WARNING) file({&quot;@id&quot;:&quot;AXWvvhh9is_Uqdy8ZoXZ&quot;,&quot;bucket&quot;:&quot;1409620614644&quot;,&quot;created&quot;:&quot;2020-11-10T01:21:12&quot;,&quot;startDate&quo........................ help
@jaberAlNahian I am guessing, the data in your file is not valid json. Either its html encoded or its not valid. Your code may help me understand further. Sorry for late reply. Also 'json.data' is a json sting containing file. if it is not clear in above example.
actually it was somehow a valid json file. all I had to do is $fileContent = preg_split("/((\r?\n)|(\r\n?))/", $fileContent); After that, I could easily use json_decode method and all data was coming fine.
0

The value can be retrieved by,

  $totalTime  = $data['route_summary']['total_time'];

If the json you pass to $data is properly formatted.

5 Comments

It's giving me NULL error again,should i use scan_dir() and loop it?
@BlackUser Probably your json is not in proper format. You can validate the json here, jsonlint.com.
It's not validating because i have placed 2 json objects in a single file.
@BlackUser Then separate the json. And with the help of loop, pass the json one-by-one. First check two jsons separately are those correct or not.
echo $data[0]['route_summary']['total_time']; and see the output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.