開始使用免費開始

查詢頂層 JSON 資料

有了 Postgres JSON,查詢半結構化資料變得相當輕鬆!Postgres 提供內建運算子,包括 ->->>。在這個例子中,你將練習使用這些運算子,從 JSON 型別的欄位查詢評論資料。此資料表的形式如下:

nested_reviews table, showing sample data.

為了讓你先行一步,已經將 pandaspd 匯入,並建立連線物件存放在變數 db_engine 中。玩得開心!

本練習屬於課程

NoSQL 入門

檢視課程

練習說明

  • 使用 -> 運算子,從 nested_reviews 資料表的 review 欄位查詢 location 欄位,並以 JSON 形式回傳。
  • nested_reviews 資料表的 review 欄位,將 statement 欄位以文字形式查詢出來。
  • 使用 pandas 執行查詢,並列印結果。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Build the query to select the review_id and rating fields
query = """
	SELECT 
    	____ -> '____' AS location, 
        ____ ____ '_____' AS statement 
    FROM ____;
"""

# Execute the query, render results
data = pd.____(____, db_engine)
print(____)
編輯並執行程式碼