始める無料で始める

ネストされた JSON キーの抽出

現場チームから、現在の気温と午前4時の気温を抽出してほしいと依頼がありました。hour_temperature キーには、午前0時(深夜)から始まる配列が入っているとのことです。JSON は今回も CTE に用意してあります。

この演習はコースの一部です

Redshift入門

コースを見る

演習の手順

  • 観測所データから現在の気温を current_temp として抽出します。
  • 観測所データから午前4時の 'hourly_temperature'four_am_temp として抽出します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

-- 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 ___;
コードを編集して実行