Reputation: 2063
This is my json file, and I would want to retrieve the value present in the key title
that is under a list. i.e LiveHealth
, OneMedical
and visit guardian
{
"data": {
"perksAndWellBeing": [
{
"title": "Physical Wellbeing",
"perks": [
{
"id": "a3N33000EAO",
"logo": "https:933000001P3v1AAC-1.png",
"logoLarge": "https:\33000001P3v6AAC-1.png",
"title": "LiveHealth Online",
},
{
"id": "a3N3300AO",
"logo": "https:\/\-attachments\/06933000001P3uwAAC-1.png",
"logoLarge": "https:/hments\/06933000001P3umAAC-1.png",
"title": "One Medical",
},
{
"id": "a3N3300EA4",
"logo": "https:\/\/s33000001P3vQAAS-1.png",
"logoLarge": "https:\/06933000001P3vVAAS-1.png",
"title": "Access To Top Specialists",
}
]
},
{
"title": "Emotional Wellbeing",
"perks": [
{
"id": null,
"logo": "https:\/\/s00001P3vpAAC-1.png",
"logoLarge": "https:\/\/attachmeOAAS-1.png",
"title": "Visit Guardian EAP",
}
]
},
{
"title": "Financial Wellbeing",
"perks": [
{
"id": "a3N3300HEA4",
"logo": "https:\/\/sg",
"logoLarge": "https:\/SAAS-1.png",
"title": "Track your finances and get free access to financial advice",
},
{
"id": "a3N33000EA4",
"logo": "https:\/\/s",
"logoLarge": "https:\/\/",
"title": "Better student loan refinancing, mortgages, and personal loans",
}
]
},
{
"title": "Perks",
"perks": [
{
"id": "a3N3300LxEAO",
"logo": "https:\/\",
"logoLarge": "https:\/,
"title": "Anthem Programs",
},
{
"id": "a3N3300000EA4",
"logo": "https:\",
"logoLarge": "https:\",
"title": "Receive gym discounts, travel discounts, and more",
}
]
}
]
}
}
Currently, I am able to retrieve the title's that has value as Physical wellbeing
and Emotional wellbeing
but I am not able to access the values in the list.
perkswellbeing = JSON.parse(perks)
#puts perkswellbeing
puts "*********************"
g = JSON.parse(perks)
result1 = g["data"]["perksAndWellBeing"].select {|g1| g1['title']}
result1.each do |rest1|
$perks = rest1['title']
puts $perks
Not sure what Am I missing! Could anyone please help me out.
Upvotes: 0
Views: 49
Reputation: 121010
json = JSON.parse(perks)
perksAWB = json["data"]["perksAndWellBeing"] # array
perks = perksAWB.map do |pawb|
pawb["perks"].map { |p| p["title"] }
end # array of titles
puts perks.inspect # printing, or:
To print everything without brackets, use flatten
:
perks.flatten.each(&method(:puts))
Upvotes: 1