Home > Net >  Trying to use json_decode in order to get data
Trying to use json_decode in order to get data

Time:10-07

They gave me this json array and i was asked to use json_decode in order to use the data for other purposes.

 $json = { "rows":[
 { id:0, data:[ "DLD224364", "13", "10.16.162.169", "4", "το ...", "2022-10-05 21:18:21" ] },
 { id:1, data:[ "DLD22364", "13", "10.16.162.164", "4", "το ...", "2022-10-05 21:17:47" ] },
 { id:2, data:[ "DLD224367", "67", "10.16.162.164", "4", "το ...", "2022-10-05 20:47:55" ] },
 { id:3, data:[ "DLD224335", "22", "10.16.162.169", "4", "το ...", "2022-10-05 20:36:03" ] },
 { id:4, data:[ "DLD224329", "38", ".162.169", "4", "το ...", "2022-10-05 20:26:55" ] },
 { id:5, data:[ "DLD024285", "58", "", "4", "το ...", "2022-10-05 17:41:08" ] },
 ] }] };

But when i am trying var_dump(json_decode($json, true)); I am getting NULL as the result.. Why is this happening ?

CodePudding user response:

To find the error that's leading to null being returned, use json_last_error_msg(); or pass the JSON_THROW_ON_ERROR option to json_decode and it will throw an exception rather than returning null.

If you do, you will find that it is complaining about invalid JSON: in JSON, all keys have to be quoted, and trailing commas aren't allowed at the end of lists.

It looks like someone's tried to write the JSON by hand, or just pasted some JavaScript code, which is not the same thing as JSON. They need to use a proper JSON serializer (e.g. JSON.stringify in JavaScript).

  • Related