Extragerea cheilor JSON de nivel superior
Echipa de teren din Idaho a revenit cu o nouă provocare: au instalat o stație meteo nouă care generează șiruri JSON, iar tu trebuie să extragi datele din ele. JSON-ul se află în CTE-ul numit weather_station, cu șirul JSON stocat în coloana data.
Acest exercițiu face parte din cursul
Introducere în Redshift
Instrucțiuni pentru exercițiu
- Extrage valoarea
'date'din coloanadatacu aliasulweather_date. - Extrage valoarea
'weather'din coloanadatacu aliasulweather_state.
Exercițiu interactiv practic
Încearcă acest exercițiu completând acest cod de exemplu.
-- 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 ___;