views:

582

answers:

4

My google Docs Spreadsheet call returns this response in the json format
(I only need everything after "rows") please look at the formatted response here : )

I use php's json_decode function to parse the data and use it (Yes, I am awful at php) This code returns NULL, and according to the documentation, NULL is returned "if the json cannot be decoded".

$json = file_get_contents($jsonurl);
$json_output = json_decode($json);

var_dump ($json_output); // Returns NULL

Basically, what i want to accomplish is to make a simple array from the first row values of the Json response.

like this

$array = {'john','John Handcock','[email protected]','2929292','blanc'}

You guys are genius, I would appreciate your insight and help on this very much!

Answer as "sberry2A" mentions bellow, the response is not valid Json, google offers the Zend Json library for this purpose, tho I decided to parse the tsv-excel version instead :)

+1  A: 

The data in the link you provided is not valid JSON. What you have provided appears to be the decoded version. You can tell that is in not JSON because the array keys are not quoted. For instance, version should be 'version'.

Your data should look more like this

'{"version":"0.6","reqId":"requestIDnumber","status":"ok","sig":"65724392","table":{"cols":[{"id":"A","label":"slug","type":"string", "pattern":""},{"id":"B","label":"name","type":"string","pattern":""},{"id":"C","label":"email","type":"string","pattern":""},{"id":"D","label" :"nsid","type":"number","pattern":"#0.###############"},{"id":"E","label":"theme","type":"string","pattern":""}],"rows":[{"c":[{"v":"mo"},{"v": "Mohammad Taheri"},{"v":"[email protected]"},{"v":"2929292.0","f":"2929292"},{"v":"blanc"}]}]}}'



$json = '{"version":"0.6","reqId":"requestIDnumber","status":"ok","sig":"65724392","table":{"cols":[{"id":"A","label":"slug","type":"string", "pattern":""},{"id":"B","label":"name","type":"string","pattern":""},{"id":"C","label":"email","type":"string","pattern":""},{"id":"D","label" :"nsid","type":"number","pattern":"#0.###############"},{"id":"E","label":"theme","type":"string","pattern":""}],"rows":[{"c":[{"v":"mo"},{"v": "Mohammad Taheri"},{"v":"[email protected]"},{"v":"2929292.0","f":"2929292"},{"v":"blanc"}]}]}}';
$data = json_decode($json);
print_r($data->table->rows);

//output

Array ( [0] => stdClass Object ( [c] => Array ( [0] => stdClass Object ( [v] => mo ) [1] => stdClass Object ( [v] => Mohammad Taheri ) [2] => stdClass Object ( [v] => [email protected] ) [3] => stdClass Object ( [v] => 2929292.0 [f] => 2929292 ) [4] => stdClass Object ( [v] => blanc ) ) ) ) 
sberry2A
hmmm, that makes sense. And yet this is what google provides as the Json response. Link to actual response -> http://bit.ly/8OXy3e Do you know anyways of overcoming this bad formatted response with a more tolerant code? Thank you very much sberry2A.
Mohammad
Are you using a published API, or did you find this call via an http proxy like fiddler, firebug, charles? From what I can tell hitting that page is returning javascript object, not json.
sberry2A
This was a documented call as far as i remember and i'm trying to find the api documentation link right now.
Mohammad
http://code.google.com/apis/visualization/documentation/dev/implementing_data_source.html#jsondatatable this is the documentation on this specific call.
Mohammad
you're right under "JSON Modifications" it actually warns that the json format is non-standard. Do you know of any simple ways of formatting this response correctly so that it would work in the php Json decoder? Thank you for your time really! : )
Mohammad
I ended up using the csv format of the code for the time being. By switching json with tsv-excel in the url, and parsing the values from there. The Zend framework is the official google partner and supports the altered Json code, tho for this simple goal i decided not to go in that territory.
Mohammad
A: 

Did you try removing the callback function from the response myData(...)?

phidah
Nope, i'll try that also : )
Mohammad
Parse error: parse error in D:\wamp\www\lalala\index.php on line 61 line 61: $json_output = json_decode($json);
Mohammad
Remember to add slashes around it. The json input is a string.
phidah
tried that, still returns the same error : ( thank you though!
Mohammad
I apologize it actually returns NULL also.
Mohammad
A: 
$json = file_get_contents($jsonfile);
$data = json_decode($json);
print_r($data);
Giles Van Gruisen
this is exactly what i used in the question, and yet this only works with correctly formatted Json, not google docs'
Mohammad
A: 

The PEAR package Services_Json is able to parse JSON with unquoted keys. So, strip the callback and parse with Services_Json and I believe that will work.

http://mike.teczno.com/JSON/doc/

ColinM
Thanks ColinM, after I realized it was a non standard implementation of Json I noticed a few packages that would handle it. They were a bit too robust so I ended up writing my own tiny script to parse the file instead.
Mohammad
Ahh. I'd like to see that if you don't mind posting it somewhere (pastebin, etc..). Thanks!
ColinM