0

I am using HTTParty to call the Flickr API. I get a response but it's escaped. How can I get plain and simple json?>

class Flickr

  require 'httparty'
  include HTTParty

  API_KEY         = '2e18b933c285b552701e7c6f5bff3aee'
  SECRET          = '88123767bb4492b2'
  USER_ID         = '57951266@N03'
  RESPONSE_FORMAT = 'json'
  base_uri          'http://api.flickr.com'

  # Get list of all collections
  def self.collections_list
    response = get("/services/rest/",
      :query => {
        method:          'flickr.collections.getTree',
        api_key:         API_KEY,
        user_id:         USER_ID,
        format:          RESPONSE_FORMAT,
        nojsoncallback:  1
      }
    )

    response.body

  end

Here is what response.body returns: How can I disable the escaping that prevents it from being valid json?

=> "{\"collections\":{\"collection\":[{\"id\":\"579244212-72157632934521433434033423\", \"title\":\"mysite.co.uk\", \"description\":\"\", \"iconlarge\":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"collection\":[{\"id\":\"5723456928212-72157634949706778\", \"title\":\"Holidays\", \"description\":\"\", \"iconlarge\":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"collection\":[{\"id\":\"5792364528212-7215763492346543457291\", \"title\":\"Standard\", \"description\":\"\", \"iconlarge\":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"set\":[{\"id\":\"72157628982366181735\", \"title\":\"Paris Trip\", \"description\":\"11th-13th May 2008 - (Peeps)\"}, {\"id\":\"72157629097324306\", \"title\":\"Ohio\", \"description\":\"August 2005 - (Peeps)\"}, {\"id\":\"72157629097297760\", \"title\":\"Iceland Holiday\", \"description\":\"5th-16th Aug 2004 - (Peeps)\"}]}, {\"id\":\"57928212-236572157629691182748\", \"title\":\"1 - Trip\", \"description\":\"\", \"iconlarge\":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"set\":[{\"id\":\"72157629683532365562\", \"title\":\"Day 1\", \"description\":\"\"}, {\"id\":\"72157629683246523541696\", \"title\":\"Day 2\", \"description\":\"\"}, {\"id\":\"7215762968234563554852\", \"title\":\"Day 3\", \"description\":\"\"}, {\"id\":\"7215762972346510465912\", \"title\":\"Day 4\", \"description\":\"\"}, {\"id\":\"72157362629710474644\", \"title\":\"Day 5 South\", \"description\":\"\"}, {\"id\":\"7215762923564710489442\", \"title\":\"Day 6 \", \"description\":\"\"}, {\"id\":\"721576292346834618028\", \"title\":\"Day 7 i\", \"description\":\"\"}, {\"id\":\"721576265349834637138\", \"title\":\"Day 8\", \"description\":\"\"}, {\"id\":\"72157629236836419418\", \"title\":\"Day 9\", \"description\":\"\"}, {\"id\":\"7215762985641123596\", \"title\":\"Day 10\", \"description\":\"\"}, {\"id\":\"72157629854631700916\", \"title\":\"Day 11 & 12\", \"description\":\"\"}, {\"id\":\"721576223649851713058\", \"title\":\"Day 13,14 & 15 -\", \"description\":\"\"}]}, {\"id\":\"57928212-72236157629866347760\", \"title\":\"2 - Trip 2010\", \"description\":\"\", \"iconlarge\":\"\/images\/collection_default_l.gif\", \"iconsmall\":\"\/images\/collection_default_s.gif\", \"set\":[{\"id\":\"721576292346866337538\", \"title\":\"Day 0 - Arrival ....

rctneil
  • 7,016
  • 10
  • 40
  • 83
  • Could consider looking at: https://github.com/hoxworth/json-schema not sure if that is what you'd be after though – Deej Oct 28 '13 at 22:43
  • `to_json` and `JSON.parse` are more or less inverse operations. I'm not sure what you are trying to do. What do you mean with "proper json"? – iltempo Oct 28 '13 at 22:56
  • I don't think that is what I am after. I wish to turn my response into valid json. json doesn't support square brackets and to hash arrows "=>" as far as I am aware. – rctneil Oct 28 '13 at 22:56

1 Answers1

1

response.to_json will give you proper JSON. JSON.parse(response.to_json) will turn that JSON into a ruby hash of hashes/arrays/etc.

Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • ah right ok. When I do response.to_json. A lot of the single and double quotes are escaped. How can this be solved? Also what is the best way of looping through this result set once I have the valid json with no escaped characters? – rctneil Oct 29 '13 at 09:16
  • Are you sure they are escaped? If you're doing this in the console that can happen, but it's only for display. As for looping through it.. really depends on what data is being returned. – Philip Hallstrom Oct 29 '13 at 16:02
  • I will update my OP now to show the json returned from Flickr that is in response.body – rctneil Oct 29 '13 at 16:04
  • @rctneil Are you sure it's being escaped? That looks an awful lot like what IRB outputs by default. What happens if you do `response.to_json.class`. I imagine you'll get a Hash. – Philip Hallstrom Oct 30 '13 at 16:32
  • My mistake. If response.body is returning a string, then just pass that into JSON.parse and you should get a Ruby hash. – Philip Hallstrom Oct 31 '13 at 17:08
  • Any more thoughts on this? – rctneil Nov 03 '13 at 13:59
  • @rctneil I think you'd be best served by opening up the rails console and stepping through each piece of the API call and result to see what type of objects you have and when. – Philip Hallstrom Nov 03 '13 at 16:38
  • I ran the API call in a Chrome extension HTTP client and it returns valid json. But when attempting the call in my rails app via HTTParty I get the escaped version. Same arguments and everything. – rctneil Nov 03 '13 at 22:33
  • Still trying to solve this. I have simplified as much as I can and it MUST be a Rails/Ruby thing as when the call is made outside of R and R, I get a nice valid response. I will update my OP with the full Flickr class I have and an example of the output I am getting. All I need is plain and simple json. Nothing more and nothing less. – rctneil Dec 18 '13 at 18:50