Mulai sekarangMulai gratis

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 merupakan bagian dari kursus

Pengantar Redshift

Lihat Kursus

Instruksi latihan

  • Ekstrak suhu saat ini SEBAGAI current_temp dari data stasiun cuaca.
  • Ekstrak 'hourly_temperature' pada pukul 4 pagi sebagai four_am_temp dari data stasiun cuaca.

Latihan interaktif langsung praktik

Cobalah latihan ini dengan melengkapi kode contoh ini.

-- 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 ___;
Edit dan Jalankan Kode