# Json =\> hash in Ruby

**URL:** <https://www.ruby-forum.com/t/json-hash-in-ruby/135832>\
**Category:** Rails\
**Created:** [April 25, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/json-hash-in-ruby/135832 "2008-04-25T13:37:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [April 25, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/json-hash-in-ruby/135832/1 "2008-04-25T13:37:20Z")

</div>

Hi everyone,

I would like to decode a json string and transform it into a hash in  
ruby.  
I tried

json = “{“userid” : “21”, “friendid” : “9”}”  
hash = ActiveSupport::JSON.decode(json)  
puts hash[:userid]

however, this gives NIL back. How do I have to do this?

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [April 25, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/json-hash-in-ruby/135832/2 "2008-04-25T14:19:52Z")

</div>

Try hash[“userid”] instead.

You can only have interchangeable strings and symbols if it is a  
HashWithIndifferentAccess, like params and flash from Rails. By the  
looks of  
that, it’s creating just a Hash.

On Fri, Apr 25, 2008 at 9:06 PM, Martin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> however, this gives NIL back. How do I have to do this?
> 
> Thanks for any help!
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![martin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/73ab20/32.png) [@martin](https://www.ruby-forum.com/u/martin)\
**Post date:** [April 25, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/json-hash-in-ruby/135832/3 "2008-04-25T19:23:39Z")

</div>

json = “{“userid” : “21”, “friendid” : “9”}”  
hash = ActiveSupport::JSON.decode(json).symbolize\_keys  
puts hash[:userid]

Note the symbolize keys which will make it work as expected with the  
keys as symbols. By default, the keys are strings:

puts hash[‘userid’]
