Geneste JSON-sleutels extraheren
Het veldteam wil nu dat je de huidige temperatuur en de temperatuur om 4.00 uur extraheert. Ze gaven aan dat de sleutel hour_temperature een array bevat die start om 12.00 uur ’s nachts (middernacht). De JSON staat weer in de CTE voor je klaar.
Deze oefening maakt deel uit van de cursus
Introductie tot Redshift
Oefeninstructies
- Extraheer de huidige temperatuur ALS
current_tempuit de weerstationgegevens. - Extraheer de
'hourly_temperature'om 4.00 uur alsfour_am_tempuit de weerstationgegevens.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
-- 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 ___;