Mengekstrak kunci JSON bersarang
Tim lapangan sekarang meminta Anda mengekstrak suhu saat ini dan suhu pada pukul 4 pagi. Mereka mengatakan kunci hour_temperature berisi sebuah array yang dimulai pada pukul 12 malam (tengah malam). JSON sudah disiapkan untuk Anda dalam CTE.
Latihan ini adalah bagian dari kursus
Pengantar Redshift
Petunjuk latihan
- Ekstrak suhu saat ini SEBAGAI
current_tempdari data stasiun cuaca. - Ekstrak
'hourly_temperature'pada pukul 4 pagi sebagaifour_am_tempdari data stasiun cuaca.
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
-- 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 ___;