+3  A: 

If you want to be anal about it:

var content = obj && obj.results && obj.results[0] && obj.results[0].content;

But I don't really think that's the problem.. that callback is only called after everything is done. And the JSON parsing is synchronous. Are you sure the server is returning the right data?

FYI, you can also provide the dataType option for jQuery.ajax calls so the JSON parsing is done automatically.

CD Sanchez
balupton
@balupton: I've rarely found the need to cast these expressions to Booleans, but out of those three I think I would prefer the `Boolean` solution first and the ternary operator version second. The `Boolean` version seems less verbose and just as clear (of course, that's assuming you know what `Boolean` does in the first place). Though with the `Boolean` method there is the slightest chance that someone is naughty and decides to replace Boolean with a bogus function or value. For the best of both worlds, you could wrap the ternary operator version in a function call, making it both secure/clear
CD Sanchez
@Daniel: Setting the dataType seems to have completely fixed it. This code did not work though: var content = obj how is the parse synchronous? Surely the ajax success function runs and then the json is parsed. I was sure that that this line - var content = obj.results[0].content;- was running before the json had parsed and that's why it was null. Thanks for the fix.
elduderino
@elduderino: The code I gave you should have only suppressed the errors at best. You can tell the parsing is synchronous because you were assigning it to a variable: `obj`. It could not have continued until the parsing was finished since it evaluates function calls. What I think the real problem was that jQuery was already parsing the JSON for you implicitly (it tries to guess based in mime type, etc). So you were passing `data`, which was already a JS object, to `parseJSON` which returns null/undefined when passed in something that isn't a JSON string.
CD Sanchez
A: 

It doesn't make sense to me why should the method parseJSON be asynchronous.

Why don't you simply write an IF statement like this:

 ...
 var obj = jQuery.parseJSON(data);
 if (obj != null && obj.results != null && jQuery.isArray(obj.results) &&
     obj.results.length > 0 && obj.results[0].content != null) {

   var content = obj.results[0].content;
   /* do some processing */
 }
MartyIX
+1  A: 

Might be worthwhile to look at what the data variable is set to, so that you know why jQuery.parseJSON is returning null:

$.ajax({ url: url, success: function(data) {
    console.log(data);
    var obj = jQuery.parseJSON(data);
    var content = obj.results[0].content;
}})

If you've not used the console before, try downloading Firebug.

You could also use the dataType option suggested by Daniel to simplify the code:

$.ajax({ url: url, dataType: "json", success: function(data) {
    var content = data.results[0].content;
}})
Douglas
Hi, Yes data is coming back fine every time. Setting the datType seems to have fixed it.
elduderino