使用 Postgres 從 JSON 物件擷取鍵
當你開始接觸半結構化資料時,常會好奇一組 JSON 物件中有哪些不同的鍵。本練習要你用 Postgres 內建的 JSON 功能來實作這件事。
和之前一樣,pandas 已經以 pd 匯入,並提供一個連線物件,可透過變數 db_engine 使用。加油,開始動手做吧!
本練習屬於課程
NoSQL 入門
練習說明
- 撰寫查詢,建立一個結果集,包含
nested_reviews資料表中review欄位所存放 JSON 物件的唯一鍵集合。 - 將結果集儲存到名為
unique_keys的變數,並輸出結果。驗證在這個 DataFrame 中共有 2 個鍵。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build a query to find the unique keys in the review column
query = """
SELECT
____ ____(____)
FROM ____;
"""
# Execute the query, show the results
____ = pd.read_sql(____, db_engine)
print(____)