162

I have a JSON object holding the following value:

@value = {"val":"test","val1":"test1","val2":"test2"}

I want to loop through it in Ruby to get the key/value pairs. When I use @each, it doesn't iterate through the object because it is not in the Ruby hash form:

@value = {"val"=>"test","val1"=>"test1","val2"=>"test2"}

How can I convert the above JSON object to a Ruby hash?

5 Answers 5

296
require 'json'
value = '{"key_1":"value_1", "key_2":"value_2"}'
puts JSON.parse(value) # => {"key_1"=>"value_1","key_2"=>"value_2"}
Sign up to request clarification or add additional context in comments.

1 Comment

value = '{"val":"test","val1":"test1","val2":"test2"}' could have been more readable.
58

You could also use Rails' with_indifferent_access method so you could access the body with either symbols or strings.

value = '{"val":"test","val1":"test1","val2":"test2"}'
json = JSON.parse(value).with_indifferent_access

then

json[:val] #=> "test"

json["val"] #=> "test"

3 Comments

Does anyone know if this is more resource-intensive for larger hash objects? I'm new to Ruby/Rails, but assuming this duplicates key-value pairs?
Oh God, this was a lifesaver after 2 hours of looking for a "simple" solution. Thank you!!
it may cause issues in your specs when trying to match stubbed requests
9

I'm surprised nobody pointed out JSON's [] method, which makes it very easy and transparent to decode and encode from/to JSON.

If object is string-like, parse the string and return the parsed result as a Ruby data structure. Otherwise generate a JSON text from the Ruby data structure object and return it.

Consider this:

require 'json'

hash = {"val":"test","val1":"test1","val2":"test2"} # => {:val=>"test", :val1=>"test1", :val2=>"test2"}
str = JSON[hash] # => "{\"val\":\"test\",\"val1\":\"test1\",\"val2\":\"test2\"}"

str now contains the JSON encoded hash.

It's easy to reverse it using:

JSON[str] # => {"val"=>"test", "val1"=>"test1", "val2"=>"test2"}

Custom objects need to_s defined for the class, and inside it convert the object to a Hash then use to_json on it.

Comments

5

Assuming you have a JSON hash hanging around somewhere, to automatically convert it into something like WarHog's version, wrap your JSON hash contents in %q{hsh} tags.

This seems to automatically add all the necessary escaped text like in WarHog's answer.

Comments

0
You can use the nice_hash gem: https://github.com/MarioRuiz/nice_hash

    require 'nice_hash'
    my_string = '{"val":"test","val1":"test1","val2":"test2"}'

    # on my_hash will have the json as a hash, even when nested with arrays
    my_hash = my_string.json

    # you can filter and get what you want even when nested with arrays
    vals = my_string.json(:val1, :val2)

    # even you can access the keys like this:
    puts my_hash._val1
    puts my_hash.val1
    puts my_hash[:val1]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.