Convert nested JSON to object in Ruby

In the past, every time I tried to convert a complex nested JSON to an object, I used to depend on some third party gems like Hashie or recursive-open-struct. Recently I found a very simple way to solve my problem which depend on json which is included in standard lib of Ruby itself.

require 'json'
require 'ostruct'

result = JSON.parse('{"foo":{"bar":[1,2]}}', object_class: OpenStruct)
result.foo.bar  # => [1, 2]

You also can learn more about other options that available to pass when you call JSON.parse