Ruby on Rails and JSON parser from URL
JSON.load
takes a source that is a String
or IO
object according to the documentation
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-load
[17] pry(main)> {hello: "World"}.to_json
=> "{\"hello\":\"World\"}"
[18] pry(main)> JSON.load(_)
=> {"hello"=>"World"}
You're giving it a String that is a URL and that's why you're getting an error. You can use open-uri
to fetch the data from the URL to then be parsed by JSON like so...
[22] pry(main)> require 'open-uri'
=> false
[23] pry(main)> JSON.load(URI.open("https://api.github.com"))
=> {"current_user_url"=>"https://api.github.com/user",
"authorizations_url"=>"https://api.github.com/authorizations",
"emails_url"=>"https://api.github.com/user/emails",
"emojis_url"=>"https://api.github.com/emojis",
"events_url"=>"https://api.github.com/events",
"feeds_url"=>"https://api.github.com/feeds",
"following_url"=>"https://api.github.com/user/following{/target}",
"gists_url"=>"https://api.github.com/gists{/gist_id}",
"hub_url"=>"https://api.github.com/hub"}
NOTE
URI.open
returns a StringIO
object which responds to read
returning the JSON data. JSON.load turns the data into a hash to be used.
To parse the JSON string you can either use JSON.load
or JSON.parse
How to parse URL for JSON use in Rails?
You should have access to the token as params[:access_token]
in the Rails controller that handles the callback.
Documentation: http://guides.rubyonrails.org/action_controller_overview.html#parameters
In rails how to get content from a json in another url
You can use Rails UJS:
<%= link_to "Something", something_path, id: "people", data: { remote: true, type: 'json' } %>
Rails UJS binds an event handler to links and forms with the data-remote="true"
attribute. The handler by default tries to get a JS format response - which we override by setting data-type="json"
.
Rails UJS fires custom events on the originating node which we can tap into:
// Put this in application.js or somewhere else in app/assets/javascripts
// not inline.
$(document).on('ajax:success', '#people', function(data, status){
var $el = $('#response');
$.each(data, function(p){
$el.append(
'<div class="name">'+ p.name + p.age + p.gender + p.lonlat + p.name +'</div>'
);
});
});
Another way to do it is by creating a js.erb
template but thats a even worse of a mess.
how to parse json url response in rails
You can do:
def notify
response = JSON('https://voguepay.com/?v_transaction_id=demo-1345109950&type=json').body
response["status"]
response["date"]
response["merchant_id"]
response["total"]
end
Turn image URL into the image after parsing JSon using Rails
What you are looking for is the image_tag
helper
So in your case:
<td><%= image_tag(roster['image_url']) %></td>
Should do the trick
Related Topics
Redirect the "Puts" Command Output to a Log File
Rails: Ensure Only One Boolean Field Is Set to True at a Time
Resque Multiple Workers in Development Mode
Best Way to Combine Fragment and Object Caching for Memcached and Rails
How to Delete Specific Characters from a String in Ruby
How to Send Mail with Rails Without a Template
How to Declare a Variable Shared Between Examples in Rspec
How to Get Request's Target Controller and Action with Rails 3
$Redis Global Variable with Ruby on Rails
How to Install Ruby Gems When Using Rvm
Rails Console - Find Where Created at = Certain Day
Is Ruby a Scripting Language or an Interpreted Language
Neither Ruby and Nor Irb Can Load .Rb File in Current Directory
Sorting: Sort Array Based on Multiple Conditions in Ruby
How to Reflect in the Database a New Belongs_To and Has_Many Relationship in Ruby on Rails