Reputation: 683
I am receiving JSON data from Omniauth and parsing it into a hash, however the data is formatted such that the user ID, changed to "USER_ID" in the code below, is the key. I need to access the data to add uid, name, email etc to make it accessible by the rails app, however I'm not sure how to do so since the key (USER_ID) will change with each user.
Any help or suggestions would be greatly appreciated.
Here is the raw JSON output:
{
"count":1,
"users":{
"123":{
"full_name":"Bob",
"email_address":"[email protected]",
"id":"123"
}
},
"results":[
{
"key":"users",
"id":"123"
}
]
}
The following is the output as it is currently processed.
{
"provider" =>"omniauth_provider",
"uid" =>"",
"info" => {
"name" =>nil,
"email" =>nil
},
"credentials" => {
"token" =>"987654321",
"expires" =>false
},
"extra" => {
"raw_info" => {
"count" =>1,
"users" => {
"USER_ID" => {
"full_name" =>"Bob"
"email_address" =>"[email protected]",
"id" =>"123"
}
},
"results" => [
{
"key" =>"users",
"id" =>"123"
}
]
}
}
}
Upvotes: 0
Views: 584
Reputation: 526
You can convert the JSON to a hash, and then use values
to access the hash values, if it's guaranteed that there's always at most 1 results return, you can use this code:
require 'json'
response = %Q(
{
"count":1,
"users":{
"123":{
"full_name":"Bob",
"email_address":"[email protected]",
"id":"123"
}
},
"results":[
{
"key":"users",
"id":"123"
}
]
}
)
h = JSON.parse(response)
user_info = h["users"].values.first
user_info["full_name"]
user_info["email_address"]
Upvotes: 1