Converting tabular data to JSON
Sometimes, data is loaded to a Postgres table using INSERT INTO
, or COPY ... FROM
commands. Other times, it's generated from an existing table or set of columns. In this exercise, you'll explore some of Postgres' built-in tooling to create a JSON object.
To help get you started, pandas
has been imported as pd
, and a connection object has been created and stored in the variable db_engine
. Good luck!
Cet exercice fait partie du cours
Introduction to NoSQL
Instructions
- Use the
row_to_json
function to convert thereview_id
,rating
, andyear_month
columns to a single column of typeJSON
. - Execute the query, and print the first ten rows of the resulting DataFrame. Inspect the table to confirm the
row_to_json
function worked as expected.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Build a query to create a JSON-object
query = """
SELECT
____(____(review_id, ____, ____))
FROM reviews;
"""
# Execute the query, and output the results
results = pd.____(____, db_engine)
print(results.____(10))