1

I'm having difficulties grabbing any of the JSON information from this URL.

I've tried other JSON snippets and they seem to work so I'm not sure if it's the way that the URL is structured or something.

Basic example below.

<?php
    $json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
    $obj = json_decode($json);
    echo "Body: " . $obj->Body;
?>
James George Dunn
  • 523
  • 1
  • 5
  • 15
  • From the looks of things you're getting a `{data : { ...}}` json, pretty standard stuff. You can use `json_decode($jsondata, true);` if you wish to work with an array instead(I know I would). – Andrei Sep 29 '15 at 13:31
  • What was one that did work? – Jonnix Sep 29 '15 at 13:32
  • Thanks Andrew. Tried setting it to true but still nothing appears. It was another example from StackOverflow, Jon but it just seems to be my URL that doesn't. – James George Dunn Sep 29 '15 at 13:35
  • It looks like there's something wrong with the returned JSON. My tests return JSON_ERROR_SYNTAX, which could mean any number of things, but basically, PHP can't decode it. – Jonnix Sep 29 '15 at 13:36
  • I'm wondering if [this answer](http://stackoverflow.com/questions/17219916/json-decode-returns-json-error-syntax-but-online-formatter-says-the-json-is-ok) might help. – Jonnix Sep 29 '15 at 13:37
  • 1
    It fails at the first data tag should be wrapped in quotes. – Jason K Sep 29 '15 at 13:40
  • I've tried a var_dump and it's returning NULL if that helps. – James George Dunn Sep 29 '15 at 13:52
  • @JasonK is right. Running `$json = '{ "data" :'. substr($json, 8);`, to replace `data` with `"data"` before the `json_decode` makes the `json_decode` run without error. However, `$obj->Body` will return an error as it does not exist. – topher Sep 29 '15 at 13:52
  • Sorry, @topher I'm not a 100% sure what you mean. Does that mean there's an issue with the JSON feed? – James George Dunn Sep 29 '15 at 13:57
  • The first issue is with the first `data` not being wrapped in quotes. After correcting this the data is `json_decode`able. However, your code is referencing `Body` which does not exist and will throw an error. – topher Sep 29 '15 at 14:03
  • I see, thanks @topher. Is there a way I can do this via PHP as I don't have access to that JSON feed. – James George Dunn Sep 29 '15 at 14:05
  • I have done a test with the "data" being quoted but when I try a foreach it only displays data | Array. Another point to note is that the feed is coming from a .NET server so I'm not sure if this would throw any issues? @topher – James George Dunn Sep 29 '15 at 14:28

1 Answers1

1

The link provided starts with

{ data :

which is valid javascript but invalid json. You can test it on http://jsonlint.com. To fix this we can replace the data with "data" :

$json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
$obj = json_decode($json);

if (json_last_error() !== JSON_ERROR_NONE) { //check if there was an error decoding json
    $json = '{ "data" :'. substr(trim($json), 8); // replace the first 8-1 characters with { "data" :
    $obj = json_decode($json); 
}

print_r($obj->data); //show contents of data

Please note that this fix is dependent on the data source e.g. if they change data to dataset. The correct measure would be to ask the developers to fix their json implementation.

topher
  • 14,465
  • 7
  • 54
  • 69