JSON support in PostgreSQL means the database can store, query, and manipulate JSON data directly inside tables, making it easier to work with flexible, semi-structured data.
Click to reveal answer
beginner
Why is JSON support useful for modern applications?
Modern apps often use data that changes shape or has nested details. JSON lets PostgreSQL store this data without strict table columns, making development faster and more flexible.
Click to reveal answer
intermediate
How does PostgreSQL handle JSON data differently than plain text?
PostgreSQL stores JSON in a special format that allows fast searching and indexing inside the JSON, unlike plain text which is just a string without structure.
Click to reveal answer
intermediate
What are some common JSON functions in PostgreSQL?
Functions like jsonb_extract_path, jsonb_set, and jsonb_array_elements help read, update, and work with JSON data inside the database easily.
Click to reveal answer
advanced
How does JSON support improve database design?
It allows mixing structured and flexible data in one place, reducing the need for many tables and complex joins, which simplifies design and speeds up queries.
Click to reveal answer
What does JSON support in PostgreSQL allow you to do?
AAutomatically convert JSON to XML
BStore and query flexible, nested data directly
CReplace all SQL queries with JSON commands
DOnly store plain text data
✗ Incorrect
PostgreSQL's JSON support lets you store and query flexible, nested JSON data directly inside tables.
Which PostgreSQL data type is optimized for JSON storage and querying?
Avarchar
Bint
Ctext
Djsonb
✗ Incorrect
The jsonb type stores JSON in a binary format optimized for indexing and fast querying.
Why might JSON support reduce the need for multiple tables?
ABecause JSON is faster than any table
BBecause JSON replaces SQL entirely
CBecause JSON can store nested data in one column
DBecause JSON automatically creates tables
✗ Incorrect
JSON can hold nested and flexible data in one column, reducing the need for many related tables.
Which function helps extract a value from a JSONB column in PostgreSQL?
Ajsonb_extract_path
Bsubstring
Cto_char
Darray_length
✗ Incorrect
jsonb_extract_path extracts a specific value from a JSONB column by following a path.
What is a key benefit of indexing JSONB data in PostgreSQL?
AFaster searches inside JSON documents
BAutomatically fixes JSON errors
CConverts JSON to XML
DPrevents data duplication
✗ Incorrect
Indexing JSONB data allows PostgreSQL to quickly search inside JSON documents.
Explain why JSON support in PostgreSQL is important for handling modern application data.
Think about how apps today use data that changes shape or has many details.
You got /4 concepts.
Describe how PostgreSQL's JSONB type improves performance compared to storing JSON as plain text.
Consider how the database can quickly find data inside JSON.
You got /4 concepts.
Practice
(1/5)
1. Why is JSON support important in PostgreSQL?
easy
A. It makes PostgreSQL faster than all other databases.
B. It allows storing flexible and varying data structures in one column.
C. It replaces the need for any relational tables.
D. It automatically converts JSON to XML format.
Solution
Step 1: Understand JSON flexibility
JSON allows storing data that can change structure without altering the table schema.
Step 2: Compare with relational tables
Relational tables require fixed columns, but JSON columns can hold different keys and values per row.
Final Answer:
It allows storing flexible and varying data structures in one column. -> Option B
Quick Check:
JSON = flexible data storage [OK]
Hint: JSON stores flexible data in one column easily [OK]
Common Mistakes:
Thinking JSON makes PostgreSQL faster than all others
2. Which of the following is the correct way to declare a JSON column in a PostgreSQL table?
easy
A. CREATE TABLE data (info XML);
B. CREATE TABLE data (info TEXT[]);
C. CREATE TABLE data (info VARCHAR);
D. CREATE TABLE data (info JSON);
Solution
Step 1: Identify JSON data type syntax
PostgreSQL uses the JSON or JSONB data type to store JSON data.
Step 2: Check each option
Only CREATE TABLE data (info JSON); uses JSON type correctly; others use unrelated types.
Final Answer:
CREATE TABLE data (info JSON); -> Option D
Quick Check:
JSON column = JSON type [OK]
Hint: Use JSON or JSONB type for JSON columns [OK]
Common Mistakes:
Using TEXT[] or VARCHAR for JSON data
Confusing JSON with XML data type
Missing JSON keyword in column definition
3. Given the table users with a JSON column profile, what does this query return?
SELECT profile->>'name' FROM users WHERE profile->>'age' = '30';
medium
A. An error because ->> cannot be used in WHERE.
B. All JSON profiles where age is 30.
C. Names of users whose age is 30 as text values.
D. Age values of users named '30'.
Solution
Step 1: Understand JSON operators
The operator ->> extracts JSON object field as text. Here, profile->>'age' gets age as text.
Step 2: Analyze query logic
The WHERE clause filters rows where age equals '30'. The SELECT returns the name field as text for those rows.
Final Answer:
Names of users whose age is 30 as text values. -> Option C
Quick Check:
->> extracts text field = names for age 30 [OK]
Hint: ->> extracts text; WHERE filters JSON text fields [OK]
Common Mistakes:
Thinking ->> cannot be used in WHERE clause
Expecting full JSON objects instead of text
Confusing age and name fields
4. What is wrong with this query that tries to filter JSON data?
SELECT * FROM orders WHERE details->'price' = 100;
medium
A. Using -> returns JSON, so comparison with integer 100 fails.
B. The table name 'orders' is invalid in PostgreSQL.
C. The operator -> cannot be used in WHERE clause.
D. JSON data cannot be filtered in PostgreSQL.
Solution
Step 1: Understand JSON operators and types
The operator -> returns JSON type, not a plain value, so comparing it directly to integer 100 causes a type mismatch.
Step 2: Correct usage for comparison
Use ->> to extract text and then cast to integer for numeric comparison.
Final Answer:
Using -> returns JSON, so comparison with integer 100 fails. -> Option A
Quick Check:
-> returns JSON, not scalar, so comparison fails [OK]
Hint: Use ->> to get text before comparing JSON values [OK]
Common Mistakes:
Comparing JSON object directly to integer
Assuming -> works like ->>
Believing JSON cannot be filtered in WHERE
5. You have a table events with a JSONB column data storing event details. You want to find all events where the nested JSON key location.city is 'Paris'. Which query correctly achieves this?
hard
A. SELECT * FROM events WHERE data->'location'->>'city' = 'Paris';
B. SELECT * FROM events WHERE data->>'location.city' = 'Paris';
C. SELECT * FROM events WHERE data->'location.city' = 'Paris';
D. SELECT * FROM events WHERE data->'location'->'city' = 'Paris';
Solution
Step 1: Access nested JSON keys correctly
To get nested keys, chain -> for JSON objects and ->> for text extraction. Here, data->'location' gets the location object, then ->>'city' extracts city as text.
Step 2: Check each option
data->>'location.city' treats 'location.city' as a single key. data->'location.city' does the same with ->. data->'location'->'city' returns JSON for 'city', causing type mismatch when compared to 'Paris'. Only data->'location'->>'city' extracts text correctly.
Final Answer:
SELECT * FROM events WHERE data->'location'->>'city' = 'Paris'; -> Option A
Quick Check:
Nested keys need chained -> and ->> [OK]
Hint: Chain -> for objects, ->> for text extraction in JSON [OK]
Common Mistakes:
Using dot notation inside ->> operator
Comparing JSON object directly to string
Not chaining operators for nested keys
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.