How do I parse JSON with Ruby on Rails? [duplicate]

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 API:

  "errorCode": 0,
  "errorMessage": "",
       "hash": "e5TEd",
       "shortKeywordUrl": "",
       "shortUrl": "",
       "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'.