Extragerea cheilor JSON imbricate
Echipa de teren ar dori acum să extragi temperatura curentă și temperatura de la ora 4 dimineața. Aceștia au menționat că cheia hour_temperature conține un array care începe de la miezul nopții (ora 0). JSON-ul se află din nou în CTE-ul pregătit pentru tine.
Acest exercițiu face parte din cursul
Introducere în Redshift
Instrucțiuni pentru exercițiu
- Extrage temperatura curentă AS
current_tempdin datele stației meteo. - Extrage valoarea
'hourly_temperature'de la ora 4 dimineața cafour_am_tempdin datele stației meteo.
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 current temperature
SELECT ___(data, ___, ___) AS current_temp,
-- Extract the hourly_temperature at 4AM
___(data, ___,___, ___) as four_am_temp
-- Use the CTE
FROM ___;