Extraindo chaves de JSON no nível superior
A equipe de campo de Idaho está de volta: eles receberam uma nova estação meteorológica que gera strings JSON e querem que você extraia dados delas. O JSON está na CTE chamada weather_station para você trabalhar. A string JSON está na coluna data.
Este exercício faz parte do curso
Introdução ao Redshift
Instruções do exercício
- Extraia o valor de
'date'dedatacomoweather_date. - Extraia o valor de
'weather'dedatacomoweather_state.
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 date value
SELECT ___(___, ___) AS ___,
-- Extract the weather value
___(___, ___) AS ___
-- Using the CTE
FROM ___;