시작하기무료로 시작하기

중첩된 JSON 키 추출하기

현장 팀에서 현재 기온과 새벽 4시 기온을 추출해 달라고 요청했어요. hour_temperature 키에는 자정(12AM)부터 시작하는 배열이 들어 있다고 했습니다. JSON은 다시 CTE에 준비되어 있어요.

이 연습은 강의의 일부입니다

Redshift 입문

강의 보기

연습 안내

  • 기상 관측소 데이터에서 현재 기온을 current_temp로 추출하세요.
  • 기상 관측소 데이터에서 4AM의 '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 ___;
코드 편집 및 실행