Question

I'm trying to load images from Flickr's API into a Ruby on Rails app, but I'm getting "Unexpected Token" on my JSON.parse() line.

I found another response here where the returned JSON had it's double quotes escaped out, and the solution was to add the .gsub thing to the end, but I'm still getting an error.

Anyone know what the problem is?

def add

@jsonresults = open("http://api.flickr.com/services/rest/?method=flickr.interestingness.getList&api_key=bb398c11934abb6d51bdd720020f6a4a&per_page=1&page=1&format=json&nojsoncallback=1").read
@images = JSON.parse(@jsonresults.to_json.gsub('\"', '"'))

end

The error:

JSON::ParserError in ImagesController#add

757: unexpected token at '"{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}"'
Was it helpful?

Solution

The json returned by the call looks fine. Change your parsing to this:

@images = JSON.parse(@jsonresults)

OTHER TIPS

That is not valid JSON. The outer set of double-quotes do not belong. This is the valid version:

'{"photos":{"page":1, "pages":500, "perpage":1, "total":500, "photo":[{"id":"8234011021", "owner":"24066605@N07", "secret":"b4c05df8c5", "server":"8341", "farm":9, "title":"Crescent Lake", "ispublic":1, "isfriend":0, "isfamily":0}]}, "stat":"ok"}'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top