Extraire des clés JSON de premier niveau
L’équipe de terrain de l’Idaho est de retour ! Elle dispose d’une nouvelle station météo qui produit des chaînes JSON, et elle souhaite que vous en extrayiez des données. Le JSON est disponible dans la CTE nommée weather_station. La chaîne JSON se trouve dans la colonne data.
Cet exercice fait partie du cours
Introduction à Redshift
Instructions
- Extrayez la valeur
'date'depuisdatasous le nomweather_date. - Extrayez la valeur
'weather'depuisdatasous le nomweather_state.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
-- 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 ___;