简体   繁体   中英

Fetch within an array of hashs a specific value ruby

I have this json.

{"schedule"=>
   "schedule_layers"=>
    [{"name"=>"Layer 1",
      "rendered_schedule_entries"=>[],
      "rendered_coverage_percentage"=>nil,
      "id"=>"ABC123",
      "start"=>"October3",
      "end"=>nil,
      "rotation_virtual_start"=>"October4",
      "rotation_turn_length_seconds"=>500000,
      "users"=>
       [{"user"=>
          {"id"=>"11111111",
           "type"=>"user_reference",
           "summary"=>"Micheal.Chi",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"22222222",
           "type"=>"user_reference",
           "summary"=>"Ash.A.Basha",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"33333333",
           "type"=>"user_reference",
           "summary"=>"Ven.Doli",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"44444444",
           "type"=>"user_reference",
           "summary"=>"Abby.Silly",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}}],
     {"name"=>"Layer 2",
      "rendered_schedule_entries"=>[],
      "rendered_coverage_percentage"=>nil,
      "id"=>"BHD432",
      "start"=>"November4",
      "end"=>"November5",
      "rotation_virtual_start"=>"November4",
      "rotation_turn_length_seconds"=>600000,
      "users"=>
       [{"user"=>
          {"id"=>"5555555",
           "type"=>"user_reference",
           "summary"=>"Hart.Shu",
           "self"=>nil,
           "html_url"=>nil}},
        {"user"=>
          {"id"=>"66666666",
           "type"=>"user_reference",
           "summary"=>"Man.Sam",
           "self"=>nil,
           "html_url"=>nil}},
        {"user"=>
          {"id"=>"77777777",
           "type"=>"user_reference",
           "summary"=>"Lie.Sigh",
           "self"=>"https://api.pagerduty.com/users/PRW1P1J",
           "html_url"=>"https://target.pagerduty.com/users/PRW1P1J"}},
        {"user"=>
          {"id"=>"88888888",
           "type"=>"user_reference",
           "summary"=>"Cry.Bye",
           "self"=>"https://api.pagerduty.com/users/P9WCM8S",
           "html_url"=>"https://target.pagerduty.com/users/P9WCM8S"}},
        {"user"=>
          {"id"=>"99999999",
           "type"=>"user_reference",
           "summary"=>"Man.Lan",
           "self"=>nil,
           "html_url"=>nil}},
        {"user"=>
          {"id"=>"00000000",
           "type"=>"user_reference",
           "summary"=>"Large.Hurry",
           "self"=>"https://api.pagerduty.com/users/PEA0OXF",
           "html_url"=>"https://target.pagerduty.com/users/PEA0OXF"}},
        {"user"=>
          {"id"=>"12345678",
           "type"=>"user_reference",
           "summary"=>"James.Harden",
           "self"=>"https://api.pagerduty.com/users/PFOE8GN",
           "html_url"=>"https://target.pagerduty.com/users/PFOE8GN"}},
        {"user"=>
          {"id"=>"8765321",
           "type"=>"user_reference",
           "summary"=>"Tommy.Tucker",
           "self"=>"https://api.pagerduty.com/users/PAPSIMG",
           "html_url"=>"https://target.pagerduty.com/users/PAPSIMG"}},
        {"user"=>
          {"id"=>"0101010",
           "type"=>"user_reference",
           "summary"=>"Code.Re",
           "self"=>"https://api.pagerduty.com/users/P8AVASJ",
           "html_url"=>"https://target.pagerduty.com/users/P8AVASJ"}},
        {"user"=>
          {"id"=>"21212121",
           "type"=>"user_reference",
           "summary"=>"Manny.Spinny",
           "self"=>nil,
           "html_url"=>nil}},
        {"user"=>
          {"id"=>"41414141",
           "type"=>"user_reference",
           "summary"=>"Snit.Cimen",
           "self"=>"https://api.pagerduty.com/users/PPTMCT1",
           "html_url"=>"https://target.pagerduty.com/users/PPTMCT1"}},
        {"user"=>
          {"id"=>"52525252",
           "type"=>"user_reference",
           "summary"=>"Dian.Ost",
           "self"=>"https://api.pagerduty.com/users/PAOJEX4",
           "html_url"=>"https://target.pagerduty.com/users/PAOJEX4"}},
        {"user"=>
          {"id"=>"65656565",
           "type"=>"user_reference",
           "summary"=>"Kev.Shy",
           "self"=>"https://api.pagerduty.com/users/P6YK4Y4",
           "html_url"=>"https://target.pagerduty.com/users/P6YK4Y4"}}],
      "restrictions"=>[]},
     {"name"=>"Layer 1",
      "rendered_schedule_entries"=>[],
      "rendered_coverage_percentage"=>nil,
      "id"=>"P6VKKZX",
      "start"=>"2017-10-09",
      "end"=>nil,
      "rotation_virtual_start"=>"2017-10-08",
      "rotation_turn_length_seconds"=>500505,
      "users"=>
       [{"user"=>
          {"id"=>"78787878",
           "type"=>"user_reference",
           "summary"=>"Lie.N",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"56565656",
           "type"=>"user_reference",
           "summary"=>"Habibi Le",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"32233223",
           "type"=>"user_reference",
           "summary"=>"Large.Pop",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"09090909",
           "type"=>"user_reference",
           "summary"=>"Timmy.Nitt",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://target.pagerduty.com/"}},
        {"user"=>
          {"id"=>"12435676",
           "type"=>"user_reference",
           "summary"=>"Bri.Barry",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"10101010",
           "type"=>"user_reference",
           "summary"=>"Dan Ostr",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"ABDUCDDD",
           "type"=>"user_reference",
           "summary"=>"Ive.Chlong",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"OHGILKAS",
           "type"=>"user_reference",
           "summary"=>"Jiff.Loss",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"AFRJIDN",
           "type"=>"user_reference",
           "summary"=>"Levy Shy",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}},
        {"user"=>
          {"id"=>"OFJDKANF",
           "type"=>"user_reference",
           "summary"=>"Patty Swice",
           "self"=>"https://api.pagerduty.com/",
           "html_url"=>"https://pagerduty.com/"}}],
      "restrictions"=>
       [{"type"=>"daily_restriction",
         "start_time_of_day"=>"08:30:00",
         "duration_seconds"=>120000}]}],
   "overrides_subschedule"=>
    {"name"=>"Overrides",
     "rendered_schedule_entries"=>[]

And I'm tasked with fetching only the summary under schedule => schedule_layers => users => user. This was my code for retrieving that data.

somefile = File.open("out.txt", "w+")
jdoc.fetch("schedule").fetch("schedule_layers").each do |jsonUser|
  somefile.puts jsonUser["users"][0]["user"]["summary"]
  somefile.puts jsonUser["users"][1]["user"]["summary"]
  somefile.puts jsonUser["users"][2]["user"]["summary"]
  somefile.puts jsonUser["users"][3]["user"]["summary"]
  somefile.puts "_" * 80
end

I realized that there the users hash had an array with so thats why I have those weird numbers. Regardless my text file shows this after running this code.

Micheal.Chi
Ashraf.A.Pasha
Ven.Doli
Abby.Silly
__________________________________________________________
Hart.Shu
Man.Sam
Lie.Sigh
Cry.Bye
__________________________________________________________

Lie.N
Habibi Le
Large.Pop
Timmy.Nitt
__________________________________________________________

I sorta understand why I'm only getting four from each section. But I want to find the real code in getting those values in the most beautifulest way.

Not exactly sure what you want, but I think this might be it.

somefile = File.open("out.txt", "w+")
jdoc.fetch("schedule").fetch("schedule_layers").each do |layer|
  layer["users"].each do |user|
    somefile.puts user["user"]["summary"]
  end
  somefile.puts "_" * 80
end

Use each to iterate over every user object within each layer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM