Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: JSON.parse returns undefined method `bytesize' for

Tags:

json

ruby

sinatra

response = Typhoeus::Request.get("http://localhost:3000/api/api_email/#{@api_id}.json")
    JSON.parse(response.body)

The response is a JSON object but I get an error when trying to parse it.

undefined method `bytesize' for 

I want to get access to the JSON object.

Error:

NoMethodError at /api/v1/a71040739d6cc50e89aff56601af67/2011-10-1
undefined method `bytesize' for {"xpto"=>{"email

"=>"[email protected]"}}:Hash

file: utils.rb location: bytesize line: 239

BacktracE:

/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/httpserver.rb in service
      si.service(req, res)
/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/httpserver.rb in run
          server.service(req, res)
/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/server.rb in block in start_thread
          block ? block.call(sock) : run(sock)

This is how is being generated:

 @api_id = params[:api_id]
 @bucket = Bucket.where(:api => @api_id)
 respond_with(@bucket, :only => [:email])

The .json file being returned contains:

[{"xpto":{"email":"[email protected]"}}]
like image 232
donald Avatar asked May 12 '11 22:05

donald


1 Answers

It's weird, as it seems that the response.body is already a Hash! (i.e. parsed JSON string), or, maybe you're seeing this in your webrick's log, and thus the problem is with generating the JSON response, rather than parsing it. The backtrace doesn't make sense :(

like image 152
Roman Avatar answered Oct 12 '22 10:10

Roman