So I'm learning ruby and i have to find specific values in a large JSON. I need to get this field but I'm not able to do it.
I have this json:
[{"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 I want the field "fecha"=>"2022-03-30T00:00:00",
And I'm trying this:
def json json
puts json[0].select {|json| json["fecha"]}
end
And the output is this: {} so i understand that I'm doing badly cuz I'm getting blank output.
Any idea what's going on there?
Thanks!
CodePudding user response:
Ok, so in order to navigate through a Hash ( Because what you shared is not a JSON itself, but a ruby hash. ), you can use the dig
method.
--> Example of usage here: https://apidock.com/ruby/Hash/dig ( I recommend reading the rails docs on it too. )
What you shared is not a valid hash either, but I attempted closing the missing brackets myself and this worked. Try this:
def json json
json.dig(0, 'prediccion', 'dia', 0, 'fecha')
end
This is assuming that you always want the first element of the root array and the first element of the dia
array.
If you wanted to fetch them dynamically, you would have to loop through them and replace the 0s with loop indexes.