0

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.

3
  • I think there's a problem with your JSON object. Commented Jul 27, 2017 at 16:26
  • @SebastiánPalma Let me recheck, give me a min Commented Jul 27, 2017 at 16:27
  • I have updated the json @SebastiánPalma Commented Jul 27, 2017 at 16:31

1 Answer 1

1
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))
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you! This works, Could you please tell me, how do I remove the [ ] from the result? I do not want to have the results inside the square brackets. I guess the .map returns the results inside the brackets?
I would want these results to be printed without the brackets [["LiveHealth Online", "One Medical", "Access To Top Specialists"], ["Visit Guardian EAP"], ["Track your finances and get free access to financial advice", "Better student loan refinancing, mortgages, and personal loans"], ["Anthem Programs", "Receive gym discounts, travel discounts, and more"]]
I have updated the answer: use flatten on the result.
Perfect! Still it was printing in one bracket, but removed it with puts perks.flatten.each(&method(:puts)).join(',') Thanks a lot
I am not sure as to why the code above, is printing the values twice? Visit Guardian EAP Track your finances and get free access to financial advice Better student loan refinancing, mortgages, and personal loans Anthem Programs Receive gym discounts, travel discounts, and more [ "Track your finances and get free access to financial advice", "Better student loan refinancing, mortgages, and personal loans", "Anthem Programs", "Receive gym discounts, travel discounts, and more"] It prints each one by one and everything once in an array, above.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.