Please note that accessing hash's element in Rails work in models. To use it on hash, you have to use OpenStruct
object. It's part of standard library in rails.
Considering, @foobar has decoded JSON as you have.
obj = OpenStruct.new(@foobar)
obj.data
#=> Hash
But, note that, obj.data.catalog_items willn't work, because that is an hash, and again not an OpenStruct object. To aid this, we have recursive-open-struct
, which will do the job for you.
Alternative solution [1]:
@foobar[:data]['catalog_items'].first['current_price']
But, ugly.
Alternative solution [2]:
Open Hash
class, use method_missing
ability as :
class Hash
def method_missing(key)
self[key.to_s]
end
end
Hope it helps. :)