how to get each value from json in ruby sinatra? - ruby

I need to extract each key-value of json and that value should save in database. but i am getting trouble in getting value of each key in json.
JSON
{
"topo": [
{
"dpid": "00:00:00:00:00:00:00:03",
"ports": [
3,
1
]
}
],
"app": "vm_migration"
}
code
post '/save_summary', :provides => :json do
begin
params = JSON.parse(request.env["rack.input"].read)
return params["topo"][0]["dpid"]
#above code return correct value
return params["topo"][0]["ports"] #this is not working
rescue Exception => e
return e.message
end
end
i don't know what's wrong with ports statement, please help me to figure out small issue.

What you are returning – params["topo"][0]["ports"] – is an array of two elements, which is one of the things you can return from a Sinatra route:
An Array with two elements: [status (Fixnum), response body (responds to #each)]
So you are trying to return a response with status 3 and body 1. The response body needs to be an object that responds to each, and 1 doesn’t. If you check your logs or console you will probably see an error undefined method `each' for 1:Fixnum. Assuming you just want to see the array in the browser, simply convert it to a string:
return params["topo"][0]["ports"].to_s
The first example works because params["topo"][0]["dpid"] is a string, and you can return strings from routes.

Related

How do i fix an implicit conversion for a ruby json code

I'm trying to save a json file with already inputed data with the code snippet below:
def read_rentals
return [] unless File.exist?('rentals.json')
rentals_json = JSON.parse(File.read('rentals.json'))
rentals_json.map do |rental|
Rental.new(rental['date'], #person[rental['person_index']], #books[rental['book_index']])
end
end
but I get the error message:
block in read_rentals': no implicit conversion from nil to integer (TypeError)
it stopped my script from running. robocop Rental.new line above as the error case.
The JWT.encode method expects a hash of claims. You should simply get the first value of the array and put that in as a parameter. There is no other way around. This is simply the current state of the ruby-jwt implementation.
I found the solution:
in my rentals.json, my person_index was set as null. i changed it to an integer, and the script was working
[
{
"date": "04/23",
"book_index": 0,
"person_index": 0
}
]

WebMock stub request with dynamic body in response

I am attempting to stub a request with WebMock and have the body of the response expect any value within a given regex; something to the tune of:
stub_request(:get, "someurl").
with(headers: {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
to_return(status: 200, body: { "data": [{ "id": /\d+/ }] }.to_json)
where id in the response can be any digit of any length. But this does not work. Does anyone know if this is possible?
The reason I am trying this is that there is a restraint that these IDs in question must be unique, and in a test environment I need to be able to create unique IDs and have the response still accept them, without having to manually, explicitly list out every possible digit in this stub_request.
I would suggest to use the lambda on to_return and handle your regex over there.
stub_request(:any, 'www.example.net').
to_return(lambda do |request|
# TODO: check your regex here
{body: request.body}
end)
RestClient.post('www.example.net', 'abc') # ===> "abc\n"

Storing JSON from API which has null values

I'm trying to save null value in JSON into postgresql. When I use the following code:
resume = Resume.create!({
parsedres: {"Resume":{"xml:lang":"en","xmlns":"http://ns.hrxml.org/2006-02-28","xmlns:vos":"http://noresm.com/hr-xml/2006-02-28","ResumeId":{"IdValue":null}}}
})
I get an error:
NameError: undefined local variable or method `null' for main:Object
The issue is I get API data without "" for null and when I use JSON.parse
resume.parsedres = JSON.parse(response.body)
resume.save
It throws the
JSON::ParserError: 765: unexpected token at '<ParseResumeResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Code>Ack</Code><SubCode>Authentication/SubCode><Message>AccountID.</Message><CreditsRemaining>100</CreditsRemaining></ParseResumeResponse>'
Why does JSON.parse not interpret null?
In the first example you provided, the parsedres is not JSON but a Ruby Hash literal. Since the literal is Ruby code, you'd need to use Ruby's nil keyword instead of null:
resume = Resume.create!({
parsedres: {"Resume":{"xml:lang":"en","xmlns":"http://ns.hrxml.org/2006-02-28","xmlns:vos":"http://noresm.com/hr-xml/2006-02-28","ResumeId":{"IdValue":nil}}}
})
If you received a JSON string from the API, you should be able to parse it using the code you listed above:
require 'json'
resume = Resume.create!({
parsedres: JSON.parse('{"Resume":{"xml:lang":"en","xmlns":"http://ns.hrxml.org/2006-02-28","xmlns:vos":"http://noresm.com/hr-xml/2006-02-28","ResumeId":{"IdValue":null}}}')
})
Based on the exception being thrown, it looks like the response.body is XML data, not JSON.

Ruby Sinatra and JSON objects from toodledo API 2.0

I have a small problem with receiving JSON objects. I'm using Ruby 1.9.3 and my goal is to receive my tasks from an API via RestClient and print them more or less pretty onto the page.
I created a route /test:
get '/test' do
json_ip_url = "http://api.toodledo.com/2/tasks/get.php?key=198196ae24792467eec09ac2191*****;modafter=1234567890;fields=folder,star,priority"
ip_details = RestClient.get(json_ip_url)
test = JSON.pretty_generate(ip_details) # => throws exception
end
The JSON#pretty_generate line throws an error, "only generation of JSON objects or arrays allowed". What am I doing wrong here?
Update:
I'am now able to output via pretty_generate, but what do I have to do, to get the elements of it. Here is the JSON Data, it seems to me its an Array with Objects inside of it?
[{"num":"18","total":"18"},{"id":"11980343","title":"Add some items to your todo list","modified":1391670256,"completed":0,"folder":"0","star":"0"},{"id":"11980345","title":"Visit the Settings section and configure your account","modified":1391670256,"completed":0,"folder":"0","star":"0"},{"id":"11980347","title":"Watch our tutorial videos in the Help section","modified":1391670256,"completed":0,"folder":"0","star":"1"},{"id":"12607789","title":"test","modified":1392285802,"completed":0,"folder":"0","star":"0"},{"id":"12636039","title":"My Task","modified":1392308705,"completed":0,"folder":"0","star":"0"},{"id":"12636041","title":"Another","modified":1392308705,"completed":0,"folder":"0","star":"1"},{"id":"12636143","title":"My Task","modified":1392308789,"completed":0,"folder":"0","star":"0"},{"id":"12636145","title":"Another","modified":1392308789,"completed":0,"folder":"0","star":"1"},{"id":"12636449","title":"My Task","modified":1392308950,"completed":0,"folder":"0","star":"0"},{"id":"12636451","title":"Another","modified":1392308950,"completed":0,"folder":"0","star":"1"},{"id":"12636621","title":"My Task","modified":1392309061,"completed":0,"folder":"0","star":"0"},{"id":"12636623","title":"Another","modified":1392309061,"completed":0,"folder":"0","star":"1"},{"id":"12636665","title":"My Task","modified":1392309085,"completed":0,"folder":"0","star":"0"},{"id":"12636667","title":"Another","modified":1392309085,"completed":0,"folder":"0","star":"1"},{"id":"12636733","title":"My Task","modified":1392309137,"completed":0,"folder":"0","star":"0"},{"id":"12636735","title":"Another","modified":1392309137,"completed":0,"folder":"0","star":"1"},{"id":"12637135","title":"My Task","modified":1392309501,"completed":0,"folder":"0","star":"0"},{"id":"12637137","title":"Another","modified":1392309501,"completed":0,"folder":"0","star":"1"}]
The Code I used for pretty_generate:
get '/save' do
jdata = params[:data]
response = RestClient.get 'http://api.toodledo.com/2/tasks/get.php?key=da21e24e2a00ba9d45008974aed00***;modafter=1234567890;fields=folder,star,priority', {:accept => :json}
test = JSON.parse(response)
test.to_json
output = JSON.pretty_generate(test)
puts output
RestClient#get returns the raw response as a string (and not a hash or array) when called without a block, so ip_details isn't a structure that JSON#pretty_generate knows how to handle. You need to use JSON#parse to turn the response into a hash or array first.

Cannot convert Hash to String?

I am trying to parse the JSON response from Wordnik's API. This is built with Sinatra. I keep getting the error "TypeError at /word" "can't convert Hash into String". Am I using the json parser incorrectly?
Here's my code:
get '/word' do
resp = Wordnik.words.get_random_word(:hasDictionaryDef => 'true', :maxCorpusCount => 20, :minLength => 10)
result = JSON.parse(resp)
word = result.word
return word.to_s
end
You are probably getting a hash. To convert it use to_json:
JSON.parse(resp.to_json)
You have not given what's the JSON response that you are parsing. But assuming it is something of the form
{
"word":"my_word"
}
you need to do result["word"] to get the value after parsing the JSON response.

Resources