Home > Back-end >  Get single value without [] with json.dig Ruby
Get single value without [] with json.dig Ruby

Time:04-01

So i have this code to get values from a json:

json.dig(0,'prediction','day','0','temperature')

And i get this output:

{"value" => "5", "period"=>"03"}

Here it is the json file:

[{"origen"=>                                                  
    {"productor"=>"Agencia Estatal de Meteorología - AEMET. Gobierno de España",
     "web"=>"https://www.aemet.es",                              "enlace"=>"https://www.aemet.es/es/eltiempo/prediccion/municipios/horas/lagran-id01030",
     "language"=>"es",                                         
     "copyright"=>"© AEMET. Autorizado el uso de la información y su reproducción citando a AEMET como autora de la misma.",
     "notaLegal"=>"https://www.aemet.es/es/nota_legal"},         
   "elaborado"=>"2022-03-30T22:43:40",                         
   "nombre"=>"Lagrán",                                         
   "provincia"=>"Araba/Álava",
   "prediccion"=>
    {"dia"=>
      [{"estadoCielo"=>
         [{"value"=>"16", "periodo"=>"20", "descripcion"=>"Cubierto"},
          {"value"=>"16n", "periodo"=>"21", "descripcion"=>"Cubierto"},
          {"value"=>"82n", "periodo"=>"22", "descripcion"=>"Bruma"},
          {"value"=>"16n", "periodo"=>"23", "descripcion"=>"Cubierto"}],
        "precipitacion"=>[{"value"=>"0", "periodo"=>"20"}, {"value"=>"0", "periodo"=>"21"}, {"value"=>"0", "periodo"=>"22"}, {"value"=>"Ip", "periodo"=>"23"}],
        "probPrecipitacion"=>[{"value"=>"5", "periodo"=>"2002"}],
        "probTormenta"=>[{"value"=>"5", "periodo"=>"2002"}],
        "nieve"=>[{"value"=>"0", "periodo"=>"20"}, {"value"=>"0", "periodo"=>"21"}, {"value"=>"0", "periodo"=>"22"}, {"value"=>"0", "periodo"=>"23"}],
        "probNieve"=>[{"value"=>"0", "periodo"=>"2002"}],
     "temperatura"=>[{"value"=>"7", "periodo"=>"21"}, {"value"=>"6", "periodo"=>"22"}, {"value"=>"6", "periodo"=>"23"}],      
        "sensTermica"=>[{"value"=>"4", "periodo"=>"21"}, {"value"=>"2", "periodo"=>"22"}, {"value"=>"3", "periodo"=>"23"}],     
        "humedadRelativa"=>[{"value"=>"81", "periodo"=>"21"}, {"value"=>"84", "periodo"=>"22"}, {"value"=>"87", "periodo"=>"23"}]
        "vientoAndRachaMax"=>
         [{"direccion"=>["N"], "velocidad"=>["15"], "periodo"=>"21"},
          {"value"=>"45", "periodo"=>"21"},
          {"direccion"=>["NO"], "velocidad"=>["19"], "periodo"=>"22"},
          {"value"=>"39", "periodo"=>"22"},
          {"direccion"=>["N"], "velocidad"=>["15"], "periodo"=>"23"},
          {"value"=>"44", "periodo"=>"23"}],

        "fecha"=>"2022-03-30T00:00:00",

        "orto"=>"07:56",
        "ocaso"=>"20:34"},

And the wished output is just 5 that comes frome value

Can do it without deletinig charecters and without dirty the code?

Thanks!

CodePudding user response:

Your wanted output should get from this:

json.dig(0,'prediction','day','0','temperature').map{|e|e['value'].to_i}

Hope this help.

  • Related