Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Jose Linares
Jose Linares
5,062 Points

Why is this JSONP invalid?

I'm looking to access some data with jQuery but I've had some trouble with this and when I checked the JSONP data I was trying to access from an API it says that it is invalid JSON. Why is that? Is this something normal? A JSONP data always returns invalid in a JSON validator? Here is the data.

response({"status":"ok","status_message":"Query was successful","data":{"id":10,"url":"https:\/\/yts.to\/movie\/snatch-2000","imdb_code":"tt0208092","title":"Snatch.","title_long":"Snatch. (2000)","slug":"snatch-2000","year":2000,"rating":8.3,"runtime":102,"genres":["Comedy","Crime"],"language":"English","mpa_rating":"R","download_count":298167,"like_count":324,"rt_critics_score":73,"rt_critics_rating":"Fresh","rt_audience_score":93,"rt_audience_rating":"Upright","description_intro":"Unscrupulous boxing promoters, violent bookmakers, a Russian gangster, incompetent amateur robbers, and supposedly Jewish jewelers fight to track down a priceless stolen diamond.","description_full":"Turkish and his close friend\/accomplice Tommy get pulled into the world of match fixing by the notorious Brick Top. Things get complicated when the boxer they had lined up gets badly beaten by Pitt, a \u0027pikey\u0027 ( slang for an Irish Gypsy)- who comes into the equation after Turkish, an unlicensed boxing promoter wants to buy a caravan off the Irish Gypsies. They then try to convince Pitt not only to fight for them, but to lose for them too. Whilst all this is going on, a huge diamond heist takes place, and a fistful of motley characters enter the story, including \u0027Cousin Avi\u0027, \u0027Boris The Blade\u0027, \u0027Franky Four Fingers\u0027 and \u0027Bullet Tooth Tony\u0027. Things go from bad to worse as it all becomes about the money, the guns, and the damned dog!","yt_trailer_code":"PvQnBwDejfw","date_uploaded":"2011-08-02 22:12:51","date_uploaded_unix":1312279971,"torrents":[{"url":"https:\/\/yts.to\/torrent\/download\/513EC39C73135C43AACACECDC78B300161902A45.torrent","hash":"513EC39C73135C43AACACECDC78B300161902A45","quality":"720p","resolution":"1280*688","framerate":23.976,"seeds":280,"peers":58,"size":"449.71 MB","size_bytes":471555113,"download_count":298167,"date_uploaded":"2011-08-02 22:12:51","date_uploaded_unix":1312279971}]},"@meta":{"server_time":1440581356,"server_timezone":"Pacific\/Auckland","api_version":2,"execution_time":"4.02 ms"}})

Michael Eaton
Michael Eaton
5,131 Points

Hey Jose,

Seems valid to me, what makes you think it's invalid? The JSON Formatter and Validator here says it's valid..

ValidJson

Jose Linares
Jose Linares
5,062 Points

Well I used another website for that and it said it was invalid, with yours it says valid. I don't know whats going on.

3 Answers

Hi Jose,

http://jsonlint.com/ seems to agree that your object is valid JSON. I had to copy inside the 'response' parentheses..

But doesn't JSONP need a parsing callback??

Take care,

-- Cal

Jose Linares
Jose Linares
5,062 Points

But does it always need a callback? The API I'm using has a link specially for JSONP, do I need the callback here too? I thought it was only necessary to tell the server that we want JSONP format?

Hi Jose,

That's a good question.. JSONP is "wrapped" and requires a callback function call to unpack the JSON data. But I do not know if jQuery takes care of that unpacking and just hands you JSON.

It would help to have wider context: Your example code, how you set up the request, what badge and video you're working on, etc. The rest of us are sort of newbies too, and so it would help to have your code to play around with.

Thanks!

-- Cal

You know, Jose, it might be interesting to set up the JSONP request and response handling outside jQuery..??

What do you think?

-- Cal