prabhu
2014-May-08  15:53 UTC
convert json string to hash and retrieve hash items values in ruby
I have a string object which is basically in a json format and while 
trying to print it shows in console as 
   
    item = 
     {
            "id": "4c9f83e4-f479-48d0-9f92-3fff70a8f6ba",
             "item": 
"{"business":"1114","class":"Demo","date":"01-01-2014","version":"","data":"dummy","name":"Finance"}"
        
        }
I need to get the values of business, class, date etc and pass it as params 
to my method. So I tried to convert it into hashes as below 
    hash_item = JSON.parse (item)
and output in console shows as 
    The converted hash item is
    {"guid"=>"4c9f83e4-f479-48d0-9f92-3fff70a8f6ba", 
"item"=>"{"business":"1114","service_class":"DataServicesBalancesRaw","start_date":"2014-05-08","calc_version":"","data":"8b698df0-3673-4057-b71a-080d254c355d^^","queue_name":"high"}"}
But when I try to access the hash value for business as 
`hash_item['item']['business']` it shows 
> "business"
 since the value of item is a String in the hash_item. I am not sure 
whether my approach is correct or not. So is there any better idea or any 
inputs to retrieve the hash values . Please help.
-- 
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/b8003b66-0872-496a-a740-7f4597b5013d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.