Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm looking for a simple way to parse JSON, extract a value and write it into a db in Rails.

Explicitly what I'm looking for is a way to extract a 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.

Thoughts? Help? Any and all would be much appreciated.

share|improve this question

7 Answers

These answers are a bit dated. Therefore I give you:

require 'json'
hash = JSON.parse string
share|improve this answer

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
share|improve this answer
5  
Newer version of Rails uses 'multi-json' gem, which uses the fastest json decoder installed in the Gemfile (e.g. oj). Therefore calling ActiveSupport::JSON will be fast if you have installed faster JSON parsers. (edit rejected therefore post as comment) – lulalala Oct 22 '12 at 1:33
As for this comment multi-json is actually being dumped. There are benchmarks out there that show multi-json is actually slower than the built in json gem and other implementation of json parsers out there. – Leo Correa Jun 9 at 0:48

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)
share|improve this answer
Taken from the documentation, the parsing is now: parsed_json = JSON.parse(your_json_string) – Dany Marcoux Apr 26 at 17:04

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
share|improve this answer

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
share|improve this answer

ruby have json parser

require 'json'
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.