Question

UPDATE: I tried

json = JSON.parse(@twitch.getStreams(game:"League of Legends").to_json)['body']['streams']
json.first("_link")

But now I'm getting no implicit conversion of String into Integer

I have a JSON and i need to find certain parameters from the JSON. This is what I receive to this request:

@twitch.getStreams(game:"League of Legends")[:body][:streams}

I get a huge JSON as below and it responds for each ID. i really only care about "viewers", "previews", and "_links", how can i filter to only those?

{"_id"=>7699557472, "game"=>"League of Legends", "viewers"=>14403, "preview"=>{"small"=>"http://static-cdn.jtvnw.net/previews-ttv/live_user_flosd-80x50.jpg", "medium"=>"http://static-cdn.jtvnw.net/previews-ttv/live_user_flosd-320x200.jpg", "large"=>"http://static-cdn.jtvnw.net/previews-ttv/live_user_flosd-640x400.jpg", "template"=>"http://static-cdn.jtvnw.net/previews-ttv/live_user_flosd-{width}x{height}.jpg"}, "_links"=>{"self"=>"https://api.twitch.tv/kraken/streams/flosd"}, "channel"=>{"mature"=>nil, "abuse_reported"=>nil, "status"=>"Boxbox", "display_name"=>"flosd", "game"=>"League of Legends", "delay"=>0, "_id"=>38881685, "name"=>"flosd", "created_at"=>"2012-12-30T06:58:18Z", "updated_at"=>"2013-12-01T18:20:51Z", "logo"=>"http://static-cdn.jtvnw.net/jtv_user_pictures/flosd-profile_image-d3615fd1a2dc6187-300x300.png", "banner"=>"http://static-cdn.jtvnw.net/jtv_user_pictures/flosd-channel_header_image-2162bf5c871a74bd-640x125.jpeg", "video_banner"=>"http://static-cdn.jtvnw.net/jtv_user_pictures/flosd-channel_offline_image-79339e326c17b718-640x360.png", "background"=>nil, "profile_banner"=>nil, "profile_banner_background_color"=>nil, "url"=>"http://www.twitch.tv/flosd", "views"=>14808980, "_links"=>{"self"=>"https://api.twitch.tv/kraken/channels/flosd", "follows"=>"https://api.twitch.tv/kraken/channels/flosd/follows", "commercial"=>"https://api.twitch.tv/kraken/channels/flosd/commercial", "stream_key"=>"https://api.twitch.tv/kraken/channels/flosd/stream_key", "chat"=>"https://api.twitch.tv/kraken/chat/flosd", "features"=>"https://api.twitch.tv/kraken/channels/flosd/features", "subscriptions"=>"https://api.twitch.tv/kraken/channels/flosd/subscriptions", "editors"=>"https://api.twitch.tv/kraken/channels/flosd/editors", "teams"=>"https://api.twitch.tv/kraken/channels/flosd/teams", "videos"=>"https://api.twitch.tv/kraken/channels/flosd/videos"}}},
Was it helpful?

Solution

json = @twitch.getStreams(game:"League of Legends")[:body][:streams]
JSON.parse(json).values_at('viewers','previews','_links')

Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top