3

I'm working getting tweets from the Twitter API in my Rails backend. I'm able to create tweets in my db and then render the tweets as a JSON. But part of the tweet is still showing up as a string. How can I convert this specific part into a Ruby hash so that I can access the key:value pairs?

  • I've tried .serializable_hash, .hash, to_json, but either the data type is wrong (needs to be an array) or it just adds more \ characters

  • .strip, .split, .gsub also didn't work

  • when I puts the string, it looks like a hash, but I'm not able to save that output

This is how the json is being rendered:

   "media": [
      "{\"id\"=>1159941035461529600, \"id_str\"=>\"1159941035461529600\", \"indices\"=>[62, 85], \"media_url\"=>\"http://pbs.twimg.com/media/EBjwOeMVUAAVOia.jpg\", \"media_url_https\"=>\"https://pbs.twimg.com/media/EBjwOeMVUAAVOia.jpg\", \"url\"=>\"https://t.asdco/mFwUSlGEpv\", \"display_url\"=>\"pic.twitter.com/mFwUSlGEpv\", \"expanded_url\"=>\"https://twitter.com/CommonBlackGirI/status/1159941046161170432/photo/1\", \"type\"=>\"photo\", \"sizes\"=>{\"thumb\"=>{\"w\"=>150, \"h\"=>150, \"resize\"=>\"crop\"}, \"medium\"=>{\"w\"=>1122, \"h\"=>819, \"resize\"=>\"fit\"}, \"large\"=>{\"w\"=>1122, \"h\"=>819, \"resize\"=>\"fit\"}, \"small\"=>{\"w\"=>680, \"h\"=>496, \"resize\"=>\"fit\"}}}"
    ]

I tried to create a method in the serializer file, but that didn't work because it's just string, not a traversable hash

  def media_url
    tweet = self.object
    tweet.media[0]
  end

I'd like to convert the string above into a Ruby hash so that I can get the media_url for each tweet

5
  • 6
    JSON.parse seems to be what you're looking for Commented Aug 12, 2019 at 19:32
  • That's a Ruby Hash being emitted raw instead of as a JSON object. Try to_json on it when rendering. media_url doesn't render, but something else does. Commented Aug 12, 2019 at 19:46
  • I tried JSON.parse, got this kind of error: JSON::ParserError (767: unexpected token at '{"screen_name"=>"KimberlyNFoster", "name"=>"Kimberly Nicole Foster", "id"=>142116606, "id_str"=>"142116606", "indices"=>[0, 16]}') Commented Aug 12, 2019 at 19:51
  • to_json added more "\" => "\"{\\\"screen_name\\\"=\\u003e\\\"KimberlyNFoster\\\", \\\"name\\\"=\\u003e\\\"Kimberly Nicole Foster\\\", \\\"id\\\"=\\u003e142116606, \\\"id_str\\\"=\\u003e\\\"142116606\\\", \\\"indices\\\"=\\u003e[0, 16]}\"" Commented Aug 12, 2019 at 19:53
  • How did you end up with that? The object at h[:"media"][0] is not valid json ( it looks like a Hash converted to a String via to_s) If you need to convert a Hash to JSON the method is to_json but you should explain how this is being generated in the first place or just use the twitter gem and not worry as much about the implementation Commented Aug 12, 2019 at 20:40

1 Answer 1

4

I think something like this would help:

def media_url
  tweet = self.object
  JSON.parse(tweet.media[0].gsub('=>', ':'))["media_url"]
end

You need to replace => with : in order for JSON.parse to work

Or

To get the complete hash you can use a more generic method like media:

def media
  tweet = self.object
  JSON.parse(tweet.media[0].gsub('=>', ':'))
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.