#> and #>>
Previously, to query nested document data with Postgres JSON, you had chained the ->
and ->>
operators together. However, when working with deeply nested data, these statements could become long and difficult to read and troubleshoot. To remedy this, Postgres offers the #>
and #>>
operators. In this example, you'll practice using these operators by querying the nested_reviews
table, which takes the form below:
pandas
has been imported as pd
, and a connection object has been created and stored in the variable db_engine
. Best of luck!
This exercise is part of the course
Introduction to NoSQL
Exercise instructions
- Use the
json_typeof()
function and the#>
operator function to find the data type of the value stored in thestatement
key of thereview
column in thenested_reviews
table. - Query the
branch
field, which is nested in thelocations
object, from thereview
column, as text. Alias the field asbranch
. - Try to return the
zipcode
field nested in thelocation
object, as text, aliasing the field aszipcode
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Attempt to query the statement, nested branch, and nested
# zipcode fields from the review column
query = """
SELECT
____(review ____ '{statement}'),,
review ____ _____ AS ____,
____
FROM nested_reviews;
"""
# Execute the query, render results
data = pd.read_sql(query, db_engine)
print(data)