提取顶层 JSON 键
爱达荷外业团队又带来了新任务。他们新增了一台气象站,会输出 JSON 字符串,想请您帮忙从中取数。JSON 已放在名为 weather_station 的 CTE 中供您使用,JSON 字符串位于 data 列。
本练习是课程的一部分
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 ___;