Extraindo chaves JSON aninhadas
A equipe de campo agora quer que você extraia a temperatura atual e a temperatura às 4h. Eles disseram que a chave hour_temperature contém um array que começa às 0h (meia-noite). O JSON está novamente na CTE para você.
Este exercício faz parte do curso
Introdução ao Redshift
Instruções do exercício
- Extraia a temperatura atual como
current_tempdos dados da estação meteorológica. - Extraia a
'hourly_temperature'às 4h comofour_am_tempa partir dos dados da estação meteorológica.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
-- 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 ___;