From time to time you may have to deal with parsing out JSON. Luckily, this is crazy easy in any recent version of Ruby and is rails independent. Given the string below:


{
  "productCount": "1",
  "errorCode": "0",
  "results": [{
    "partNumber": "3245-123",
    "name": "ABC Widget 2.0",
    "price": "2.95"
  }]
}

We can quickly and easily parse this string using:


require 'json'
h = JSON.parse string

In the above code example this would return a ruby hash that mimics the JSON structure. This means that you can do stuff like this:


h["results"][0]["name"] # => "ABC Widget 2.0"

This pulls the first name of the first product in the results section. That's it! Thanks for reading!