擷取最上層的 JSON 鍵
Idaho 的外勤團隊又有新任務。他們新裝了一個氣象站,會輸出 JSON 字串,想請你幫忙從中擷取資料。JSON 已放在名為 weather_station 的 CTE 裡供你操作。data 欄位中包含 JSON 字串。
本練習屬於課程
Redshift 入門
練習說明
- 從
data中擷取'date'的值為weather_date。 - 從
data中擷取'weather'的值為weather_state。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- 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 ___;