I am getting the following data after API request and not sure how to get link from the data. Anyone help?
#<ExampleApi::Results::GetTemporaryLinkResult:0x00007f9edf280c78
#data={"metadata"=>{"name"=>"test.jpg", "id"=>"id:xxxxxxxx",
"link"=>"https://example.com/apitl/1/AnHQ08SIuxh9cdXL1q-EpG8L"}>
Its a Hash and you can retrieve by the key
#data["metadata"]["link"]
Edited
class ExamplerResult
attr_accessor :data
def initialize()
#data = {"metadata"=>{"name"=>"test.jpg", "id"=>"id:xxxxxxxx", "link"=>"https://example.com/apitl/1/AnHQ08SIuxh9cdXL1q-EpG8L"}}
end
end
data = ExamplerResult.new.data
data["metadata"]["link"]
For Hash you can use dig from ruby 2.3
#data.dig("metadata", "link") to retrieve (nested) keys
Reference link
https://ruby-doc.org/core-2.3.0_preview1/Hash.html#method-i-dig
I am using FOOD2FORK api to extract data using HTTParty in ruby.
My code :
require 'httparty' #Note : This file is in the models >> filename.rb
#Don't need to require when using bundler
#Restful Web Services
#1. Base URI 2. Support XML or JSON 3. Support HTTP operations (GET, POST)
#Think of web at MVC : website you can get resources in any format
#HTTParty parses XML or JSON for you (like your browser - it's a client). Parses into a Ruby hash or array
class Recipe
include HTTParty
ENV["FOOD2FORK_KEY"] = "5b6b74c6cc0fa9dc23871a7ae753f6c7"
base_uri "https://food2fork.com/api" #Same across most requests
default_params key: ENV["FOOD2FORK_KEY"], fields: "image_url" #defaults, like API developer key #fields: "image_url, source_url, title",
format :json #Tell it which format data comes in
#q:"search" request parameter
def self.for (keyword)
#class method called for which takes in a term
get("/search", query: {q: keyword})["recipes"] #get is method provided by HTTParty
#returns array where each element in the array is a hash
end
pp Recipe.for "chocolate"
end
It's returning me
[
{
"publisher"=>"BBC Good Food",
"f2f_url"=>"http://food2fork.com/view/9089e3",
"title"=>"Cookie Monster cupcakes",
"source_url"=>"http://www.bbcgoodfood.com/recipes/873655/cookie-monster-cupcakes",
"recipe_id"=>"9089e3",
"image_url"=>"http://static.food2fork.com/604133_mediumd392.jpg",
"social_rank"=>100.0,
"publisher_url"=>"http://www.bbcgoodfood.com"
}
]
But I only want to extract it's image_url even on using field it's extracting the whole data set any idea how to extract only image_url ?
P.S You can check the format of JSON here -
http://food2fork.com/api/search?key=65987bf1f4b22007c365c243f5670f35&q=shredded%20chicken
Below code should work
response = your response from API
response_data = JSON.parse(response.body).with_indifferent_access
response_data['recipes'].each do |recipe|
puts recipe['image_url'] # image_url of block
end
I have a controller in ruby on rails 4 and i need to pre-process its params during request processing.
I receive parameters:
{"_json"=>[{"date"=>"9/15/2014", "name"="James"},{"date"=>"2/11/2014","name"=>"John"}]}
And i need to iterate through all json array elements and update name parameter by adding 'User' post fix. So, finally my json should be:
[{"date"=>"9/15/2014", "name"="James **User**"},{"date"=>"2/11/2014","name"=>"John **User**"}]
How can i do it in my controller?
You can try this way
params = {"_json"=>[{"date"=>"9/15/2014", "name"=>"James"},{"date"=>"2/11/2014","name"=>"John"}]}
Then modify params using using
params["json"].each { |h| h["name"] = "#{h['name']} **User**" }
puts params["_json"]
if you want to preprocess for each action then used before_filter
In my controller I have the following code...
response = HTTParty.get('https://graph.facebook.com/zuck')
logger.debug(response.body.id)
I am getting a NoMethodError / undefined method `id'
If I do...
logger.debug(response.body)
It outputs as it should...
{"id":"4","name":"Mark Zuckerberg","first_name":"Mark","last_name":"Zuckerberg","link":"http:\/\/www.facebook.com\/zuck","username":"zuck","gender":"male","locale":"en_US"}
One would think it's response.body.id, but obviously that's not working. Thanks in advance!
Try this:
body = JSON.parse(response.body)
id = body["id"]
For this kind of thing, I'd recommend either a) using Koala or b) create a class using httparty. You can then set format: json to auto parse the returned json. See here and here
You can force the response to be treated as JSON using HTTParty.get like so:
response = HTTParty.get("http://itunes.apple.com/search",
{query: {term: 'tame impala'}, format: :json})
response['results'][0]['trackName']
=> "Let It Happen"
You can use response['id'] in case that the response Content-Type is application/json or also response.parse_response to get a Hash generated from the JSON payload.
response = HTTParty.get('https://graph.facebook.com/zuck')
payload = response.parsed_response
logger.debug(payload['id'])
Note: parsed_response is a Hash, only if the response Content-Type is application/json, otherwise HTTParty will return it as a string. For enforcing a Hash, in case the response does not return application/json, you can pass the format as a parameter HTTParty.get(url, format: :json).
HTTParty should automatically parse the content based on the content type returned. Something fishy seems to be going on with zuck's json.
pry(main)> HTTParty.get('https://graph.facebook.com/zuck')
=> "{\"id\":\"4\",\"first_name\":\"Mark\",\"gender\":\"male\",\"last_name\":\"Zuckerberg\",\"link\":\"https:\\/\\/www.facebook.com\\/zuck\",\"locale\":\"en_US\",\"name\":\"Mark Zuckerberg\",\"username\":\"zuck\"}"
But this works OK:
pry(main)> HTTParty.get('http://echo.jsontest.com/foo/bar/baz/foo')
=> {"baz"=>"foo", "foo"=>"bar"}
Don't forget to require 'httparty' if you're trying that in the console yourself.
This question already has answers here:
Parsing a JSON string in Ruby
(8 answers)
Closed 4 years ago.
I'm looking for a simple way to parse JSON, extract a value and write it into a database in Rails.
Specifically what I'm looking for, is a way to extract shortUrl from the JSON returned from the bit.ly API:
{
"errorCode": 0,
"errorMessage": "",
"results":
{
"http://www.foo.com":
{
"hash": "e5TEd",
"shortKeywordUrl": "",
"shortUrl": "http://bit.ly/1a0p8G",
"userHash": "1a0p8G"
}
},
"statusCode": "OK"
}
And then take that shortUrl and write it into an ActiveRecord object associated with the long URL.
This is one of those things that I can think through entirely in concept and when I sit down to execute I realize I've got a lot to learn.
These answers are a bit dated. Therefore I give you:
hash = JSON.parse string
Rails should automagically load the json module for you, so you don't need to add require 'json'.
Parsing JSON in Rails is quite straightforward:
parsed_json = ActiveSupport::JSON.decode(your_json_string)
Let's suppose, the object you want to associate the shortUrl with is a Site object, which has two attributes - short_url and long_url. Than, to get the shortUrl and associate it with the appropriate Site object, you can do something like:
parsed_json["results"].each do |longUrl, convertedUrl|
site = Site.find_by_long_url(longUrl)
site.short_url = convertedUrl["shortUrl"]
site.save
end
This answer is quite old. pguardiario's got it.
One site to check out is JSON implementation for Ruby. This site offers a gem you can install for a much faster C extension variant.
With the benchmarks given their documentation page they claim that it is 21.500x faster than ActiveSupport::JSON.decode
The code would be the same as Milan Novota's answer with this gem, but the parsing would just be:
parsed_json = JSON(your_json_string)
Here is an update for 2013.
Ruby
Ruby 1.9 has a default JSON gem with C extensions. You can use it with
require 'json'
JSON.parse ''{ "x": "y" }'
# => {"x"=>"y"}
The parse! variant can be used for safe sources. There are also other gems, which may be faster than the default implementation. Please refer to multi_json for the list.
Rails
Modern versions of Rails use multi_json, a gem that automatically uses the fastest JSON gem available. Thus, the recommended way is to use
object = ActiveSupport::JSON.decode json_string
Please refer to ActiveSupport::JSON for more information. In particular, the important line in the method source is
data = MultiJson.load(json, options)
Then in your Gemfile, include the gems you want to use. For example,
group :production do
gem 'oj'
end
This can be done as below, just need to use JSON.parse, then you can traverse through it normally with indices.
#ideally not really needed, but in case if JSON.parse is not identifiable in your module
require 'json'
#Assuming data from bitly api is stored in json_data here
json_data = '{
"errorCode": 0,
"errorMessage": "",
"results":
{
"http://www.foo.com":
{
"hash": "e5TEd",
"shortKeywordUrl": "",
"shortUrl": "http://whateverurl",
"userHash": "1a0p8G"
}
},
"statusCode": "OK"
}'
final_data = JSON.parse(json_data)
puts final_data["results"]["http://www.foo.com"]["shortUrl"]
Ruby's bundled JSON is capable of exhibiting a bit of magic on its own.
If you have a string containing JSON serialized data that you want to parse:
JSON[string_to_parse]
JSON will look at the parameter, see it's a String and try decoding it.
Similarly, if you have a hash or array you want serialized, use:
JSON[array_of_values]
Or:
JSON[hash_of_values]
And JSON will serialize it. You can also use the to_json method if you want to avoid the visual similarity of the [] method.
Here are some examples:
hash_of_values = {'foo' => 1, 'bar' => 2}
array_of_values = [hash_of_values]
JSON[hash_of_values]
# => "{\"foo\":1,\"bar\":2}"
JSON[array_of_values]
# => "[{\"foo\":1,\"bar\":2}]"
string_to_parse = array_of_values.to_json
JSON[string_to_parse]
# => [{"foo"=>1, "bar"=>2}]
If you root around in JSON you might notice it's a subset of YAML, and, actually the YAML parser is what's handling JSON. You can do this too:
require 'yaml'
YAML.load(string_to_parse)
# => [{"foo"=>1, "bar"=>2}]
If your app is parsing both YAML and JSON, you can let YAML handle both flavors of serialized data.
require 'json'
out=JSON.parse(input)
This will return a Hash
require 'json'
hash = JSON.parse string
work with the hash and do what you want to do.
The Oj gem (https://github.com/ohler55/oj) should work. It's simple and fast.
http://www.ohler.com/oj/#Simple_JSON_Writing_and_Parsing_Example
require 'oj'
h = { 'one' => 1, 'array' => [ true, false ] }
json = Oj.dump(h)
# json =
# {
# "one":1,
# "array":[
# true,
# false
# ]
# }
h2 = Oj.load(json)
puts "Same? #{h == h2}"
# true
The Oj gem won't work for JRuby. For JRuby this (https://github.com/ralfstx/minimal-json) or this (https://github.com/clojure/data.json) may be good options.
RUBY is case sensitive.
require 'json' # json must be lower case
JSON.parse(<json object>)
for example
JSON.parse(response.body) # JSON must be all upper-case
Here's what I would do:
json = "{\"errorCode\":0,\"errorMessage\":\"\",\"results\":{\"http://www.foo.com\":{\"hash\":\"e5TEd\",\"shortKeywordUrl\":\"\",\"shortUrl\":\"http://b.i.t.ly/1a0p8G\",\"userHash\":\"1a0p8G\"}},\"statusCode\":\"OK\"}"
hash = JSON.parse(json)
results = hash[:results]
If you know the source url then you can use:
source_url = "http://www.foo.com".to_sym
results.fetch(source_url)[:shortUrl]
=> "http://b.i.t.ly/1a0p8G"
If you don't know the key for the source url you can do the following:
results.fetch(results.keys[0])[:shortUrl]
=> "http://b.i.t.ly/1a0p8G"
If you're not wanting to lookup keys using symbols, you can convert the keys in the hash to strings:
results = json[:results].stringify_keys
results.fetch(results.keys[0])["shortUrl"]
=> "http://b.i.t.ly/1a0p8G"
If you're concerned the JSON structure might change you could build a simple JSON Schema and validate the JSON before attempting to access keys. This would provide a guard.
NOTE: Had to mangle the bit.ly url because of posting rules.
You can try something like this:
def details_to_json
{
:id => self.id,
:credit_period_type => self.credit_period_type,
:credit_payment_period => self.credit_payment_period,
}.to_json
end