Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ruby formatted json string to json hash in ruby?

Tags:

ruby

I want to access json string like hash object so that i can access json using key value like temp["anykey"]. How to convert ruby formatted json string into json object?

I have following json string

temp = '{"accept"=>"*/*", "host"=>"localhost:4567", "version"=>"HTTP/1.1", 
       "user_agent"=>"curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3", 
       "http_token"=>"375fe428b1d32787864264b830c54b97"}'
like image 736
Maddy Chavda Avatar asked May 14 '14 05:05

Maddy Chavda


1 Answers

Do you know about JSON.parse ?

require 'json'

my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"
like image 67
xlembouras Avatar answered Oct 05 '22 01:10

xlembouras