1

I'm learning to use API's and started with Kimonify extracting some page data from Amazon. In the view the data is coming through via the @response variable.

How can I parse this and just show the Author Name and Rank which is #22?

class HomeController < ApplicationController
  def index
   require 'rubygems'
   require 'json'
   require 'rest_client'

   @response = RestClient.get 'https://www.kimonolabs.com/api/83x1k5ua?apikey=Gsj16resq87I8wRiaVjOrtzrs0WbAhZr'

 end
end

index.html.erb

<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<p> <%= @response %> </p>

{ "name": "Tim Weed Sales Rank Colonial", 
  "count": 1, 
  "frequency": 
  "Every 15 mins", 
  "version": 11, 
  "newdata": true, 
  "lastrunstatus": "success", 
  "lastsuccess": "Tue Dec 23 2014 17:28:30 GMT+0000 (UTC)",
  "thisversionstatus": "success", 
  "nextrun": "Tue Dec 23 2014 17:43:30 GMT+0000 (UTC)", 
  "results": { "Tim Weed Sales Rank Colonial": [ { "Title": "Will Poole's Island [Kindle Edition]", 
               "Colonial Historical Fiction Rank": "#22" } ] 
             } 
  }
Bono
  • 4,757
  • 6
  • 48
  • 77
PatGW
  • 369
  • 6
  • 19
  • Looks like you're getting JSON back. See this post if you need to learn how to parse JSON. http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-ruby-on-rails – NM Pennypacker Dec 23 '14 at 18:17
  • Thank you @NickM. This article was also really helpful. http://stackoverflow.com/questions/6284743/covert-json-string-to-json-array-in-rails – PatGW Dec 29 '14 at 20:58

1 Answers1

0

Found an up to date link to an article on parsing a JSON response.

Convert JSON String to JSON Array in rails?

Update below

response = RestClient.get 'https://www.kimonolabs.com/api/83x1k5ua?apikey=Gsj16resq87I8wRiaVjOrtzrs0WbAhZr'

Took response and parsed it using the line below. Then stored the parsed JSON in the @json variable also shown below.

@json = JSON.parse(response)

To then show this in my view I used the code below. Kimonify separates these items into results, collections and properties etc.

<p> <%= @json["results"]["collection1"][0]["property1"]["href"] %> </p>
Community
  • 1
  • 1
PatGW
  • 369
  • 6
  • 19