The server is using JSON API which returns a nested data structure. I have tried to parse it using JSON.parse
but it is converted the json string to string hash by default.
Sample Data
{
"data"=>
{
"id"=>"1",
"type"=>"users",
"attributes"=>
{
"email"=>"tia_heller@lebsack.info",
"name"=>"Tanner Kreiger"
}
}
}
I have tried code below but it only convert one level deep (not children hash)
def json_body
str_hash = JSON.parse(response.body)
str_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end
I have also tried symbolize_keys
from Rails which only convert the first level as well (see :data
and the rest is the same),
{:data=>{"id"=>"1", "type"=>"users", "attributes"=>{"email"=>"darrion_hackett@weberharvey.io", "name"=>"Cleo Braun"}}}
What is the best approach to recursively convert the nested string hash into symbol hash?
Desired Result
All the value can be access using symbol, like json_response[:data][:attributes]
.