-3

Background:

I have a project to move data accessed via an API to another application. The code is in PHP and at the moment I cannot access the "url" within the returned JSON string.

$json = '{"data": [{"vehicle": {"id": "2122233","name": "MY REG"},"files": [{"vin": "WMARR44444W146311","createdAtTime": "2020-08-05T00:25:47.388Z","url": "https://tachograph-files43679/2020/8/5/WMA24XZZ7BW146311/1596587147388-b6785431-c2e1-41ff-b7af-9bb078654882.ddd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-XXXXXXXXXXXXX","id": "XXXXXXXXX"}]}],"pagination": {"endCursor": "XXXXX-be93-44b2-91a2-095af9f1c301","hasNextPage": false}}';

Here is the JSON string (sensitive data removed). Once I get the result I then have to use the URL in another call to retrieve the file data.

If I use json_decode($json) I get an error.

If I use jsondecode($json,true) the URL is not accessible.

I know that the problem is an object within the JSON string but I can't figure out how to access it.

Ultimately I will have multiple vehicles and multiple files referenced in the JSON.

So, I would like to loop through the JSON and then make another call to download the data file.

Any help appreciated.

Thanks Malcolm

Malcolmf
  • 75
  • 8

1 Answers1

1

The JSON you've provided is fine and doesn't generate an error when PHP tries to decode it. You can access the URL like this:

$json = '{"data": [{"vehicle": {"id": "2122233","name": "MY REG"},"files": [{"vin": "WMARR44444W146311","createdAtTime": "2020-08-05T00:25:47.388Z","url": "https://tachograph-files43679/2020/8/5/WMA24XZZ7BW146311/1596587147388-b6785431-c2e1-41ff-b7af-9bb078654882.ddd?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-XXXXXXXXXXXXX","id": "XXXXXXXXX"}]}],"pagination": {"endCursor": "XXXXX-be93-44b2-91a2-095af9f1c301","hasNextPage": false}}';
$obj = json_decode($json);
echo $obj->data[0]->files[0]->url;

Working demo: http://sandbox.onlinephpfunctions.com/code/9957b8173821d91a2616981788d7d1ebde959df7

You also claimed that using json_encode($data, true); makes the URL inaccessible. It doesn't. But because that command generates an associative array instead of an object, you have to use different syntax to access it:

$obj = json_decode($json, true);
echo $obj["data"][0]["files"][0]["url"];

Working demo: http://sandbox.onlinephpfunctions.com/code/3d88cde32b31cffe065b7a56c2bc57f2a46fbbe8

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Thank you very much. I do struggle with multi-dimensional arrays . – Malcolmf Aug 10 '20 at 10:57
  • @Malcolmf No problem. P.S. Conceptually I think you'd be better to think of this data as a hierarchical structure of lists and items (somewhat like a tree) than a multidimensional array. – ADyson Aug 10 '20 at 12:10
  • Thanks for that advice and sorry for my ignorance... should there be multiple files to access e.g. $obj["data"][0]["files"][1]["url"] and $obj["data"][0]["files"][2]["url"] etc. , how would I iterate through the JSON? – Malcolmf Aug 10 '20 at 12:38
  • well, once you've decoded it, it isn't JSON anymore, it's an array. So you iterate through it like any other array in PHP - e.g. with a `for` or `foreach` loop over the array items. – ADyson Aug 10 '20 at 13:01