Extraer claves JSON anidadas
El equipo de campo quiere ahora que extraigas la temperatura actual y la temperatura a las 4 a. m. Dijeron que la clave hour_temperature contiene un array que empieza a las 12 a. m. o medianoche. El JSON está en la CTE para ti de nuevo.
Este ejercicio forma parte del curso
Introducción a Redshift
Instrucciones del ejercicio
- Extrae la temperatura actual como
current_tempde los datos de la estación meteorológica. - Extrae la
'hourly_temperature'a las 4 a. m. comofour_am_tempde los datos de la estación meteorológica.
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
-- weather_station CTE with the JSON in the data column
WITH weather_station AS (
SELECT '
{
"location": "Salmon Challis National Forest",
"date": "2024-02-10",
"weather": "Rainy",
"temperature": {
"current": 10,
"min": 8,
"max": 12,
"hourly_temperature": [8, 8, 9, 9, 10, 10, 11, 11, 12]
}
}'::SUPER::VARCHAR as data
-- Above line casts to SUPER and then to
-- VARCHAR to ensure it's ready for parsing
)
-- Extract the current temperature
SELECT ___(data, ___, ___) AS current_temp,
-- Extract the hourly_temperature at 4AM
___(data, ___,___, ___) as four_am_temp
-- Use the CTE
FROM ___;