A JSON column stores JSON (JavaScript Object Notation) data directly in a table, allowing you to save structured data like objects or arrays inside a single column.
Click to reveal answer
beginner
How do you create a JSON column in a PostgreSQL table?
Use the CREATE TABLE statement with a column type json or jsonb. For example: CREATE TABLE example (data jsonb);
Click to reveal answer
intermediate
What is the difference between json and jsonb types in PostgreSQL?
json stores JSON data as text, preserving formatting. jsonb stores JSON in a binary format, which is faster for searching and indexing.
Click to reveal answer
beginner
Can you insert JSON data directly into a JSON column?
Yes, you can insert JSON data as a string. For example: INSERT INTO example (data) VALUES ('{"name": "Alice", "age": 30}');
Click to reveal answer
intermediate
Why use JSON columns instead of multiple regular columns?
JSON columns let you store flexible, nested data without changing the table structure. This is useful when data varies or has many optional fields.
Click to reveal answer
Which PostgreSQL data type is best for storing JSON data with efficient querying?
Ajsonb
Btext
Cvarchar
Djsontext
✗ Incorrect
The jsonb type stores JSON in a binary format optimized for querying and indexing.
How do you define a JSON column named 'info' in a new table?
ACREATE TABLE t (info jsonb);
BCREATE TABLE t (info string);
CCREATE TABLE t (info jsontext);
DCREATE TABLE t (info varchar);
✗ Incorrect
Use jsonb or json as the column type to store JSON data.
Which statement is true about the json type?
AIt automatically indexes JSON keys.
BIt stores JSON in binary format.
CIt cannot store arrays.
DIt stores JSON as plain text preserving formatting.
✗ Incorrect
json stores JSON as text, keeping the original formatting.
What is a benefit of using JSON columns in a database?
AFaster than all relational columns for all queries.
BStore flexible, nested data without changing table schema.
CAutomatically encrypts data.
DPrevents any data duplication.
✗ Incorrect
JSON columns allow flexible data storage without altering table structure.
How do you insert JSON data into a JSON column?
AINSERT INTO table (col) VALUES (jsonb_parse);
BINSERT INTO table (col) VALUES (key: value);
CINSERT INTO table (col) VALUES ('{"key": "value"}');
DINSERT INTO table (col) VALUES (json_object);
✗ Incorrect
JSON data is inserted as a string with proper JSON syntax.
Explain how to create a JSON column in PostgreSQL and why you might choose jsonb over json.
Think about storage format and query performance differences.
You got /4 concepts.
Describe a real-life scenario where using a JSON column in a database table is helpful.
Imagine storing user profiles with optional fields.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a JSON column in a PostgreSQL table?
easy
A. To enforce strict relational data types
B. To create indexes automatically
C. To speed up numeric calculations
D. To store flexible, structured data inside a single column
Solution
Step 1: Understand JSON column usage
JSON columns allow storing data that can have varying structure, unlike fixed columns.
Step 2: Compare with other options
Options A, C, and D do not describe JSON columns' purpose; they relate to other database features.
Final Answer:
To store flexible, structured data inside a single column -> Option D
Quick Check:
JSON columns = flexible data storage [OK]
Hint: JSON columns store flexible data, not fixed types [OK]
Common Mistakes:
Thinking JSON columns enforce strict types
Confusing JSON with indexing features
Assuming JSON speeds numeric calculations
2. Which of the following is the correct syntax to create a table with a JSON column named data?
easy
A. CREATE TABLE mytable (id INT, data JSONB STRING);
B. CREATE TABLE mytable (id INT, data JSON);
C. CREATE TABLE mytable (id INT, data TEXT JSON);
D. CREATE TABLE mytable (id INT, data STRING);
Solution
Step 1: Recall JSON column syntax
In PostgreSQL, JSON columns are declared with type JSON or JSONB directly.
Step 2: Check each option
CREATE TABLE mytable (id INT, data JSON); uses correct syntax. Options A, B, and C use invalid or mixed types.
Final Answer:
CREATE TABLE mytable (id INT, data JSON); -> Option B
Quick Check:
JSON column syntax = CREATE TABLE mytable (id INT, data JSON); [OK]
Hint: Use JSON or JSONB as column type directly [OK]
Common Mistakes:
Using STRING or TEXT with JSON incorrectly
Combining JSON with other types in declaration
Missing commas or parentheses
3. Given the table created as CREATE TABLE products (id SERIAL, info JSONB); and the insert statement: INSERT INTO products (info) VALUES ('{"name": "Pen", "price": 1.5}'); What will be the result of the query: SELECT info->>'name' FROM products WHERE id = 1;
medium
A. {"name": "Pen", "price": 1.5}
B. 1.5
C. Pen
D. NULL
Solution
Step 1: Understand JSONB insert and query
The JSONB column stores the JSON object. The operator ->> extracts the text value of the key.
Step 2: Analyze the query
The query selects the 'name' field as text from the JSONB column for id 1, which is 'Pen'.
Final Answer:
Pen -> Option C
Quick Check:
info->>'name' = 'Pen' [OK]
Hint: Use ->> to get JSON text value by key [OK]
Common Mistakes:
Using -> instead of ->> returns JSON, not text
Expecting full JSON instead of single value
Not matching the correct id in WHERE clause
4. You try to create a table with a JSON column using: CREATE TABLE users (id INT, profile JSONB DEFAULT {}); But get a syntax error. What is the problem?
medium
A. Missing quotes around the default JSON value
B. JSONB columns cannot have default values
C. Empty braces {} are not valid default for JSONB
D. INT type cannot be used with JSONB columns
Solution
Step 1: Check default value syntax for JSONB
Default JSON values must be valid JSON strings, so '{}' must be in single quotes.
Step 2: Identify the error cause
Using {} without quotes causes syntax error. Correct is DEFAULT '{}'.
Final Answer:
Missing quotes around the default JSON value -> Option A
Quick Check:
Default JSON must be quoted [OK]
Hint: Always quote JSON default values as strings [OK]
Common Mistakes:
Using unquoted braces {} as default
Assuming JSONB cannot have defaults
Confusing data types in table definition
5. You want to create a table orders with a JSONB column details that stores order info. You also want to ensure each order has a customer_id and a details JSON with a total field as a number. Which approach correctly creates the table and enforces the total field presence?
hard
A. CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT NOT NULL, details JSONB NOT NULL CHECK (details->>'total' IS NOT NULL));
B. CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT, details JSONB DEFAULT '{}');
C. CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT NOT NULL, details JSONB NOT NULL);
D. CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT NOT NULL, details JSONB NOT NULL CHECK (details ? 'total'));
Solution
Step 1: Understand JSONB constraints
To ensure 'total' key exists and is not null, use a CHECK with details->>'total' IS NOT NULL.
Step 2: Evaluate options
CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT NOT NULL, details JSONB NOT NULL CHECK (details ? 'total')); checks only key presence (details ? 'total'), but does not check value is not null. CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT NOT NULL, details JSONB NOT NULL CHECK (details->>'total' IS NOT NULL)); checks both key and value presence correctly.
Final Answer:
CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT NOT NULL, details JSONB NOT NULL CHECK (details->>'total' IS NOT NULL)); -> Option A
Quick Check:
CHECK ensures 'total' key with value [OK]
Hint: Use CHECK with details->>'key' IS NOT NULL to enforce JSON key presence [OK]
Common Mistakes:
Using ? operator without checking value
Not adding NOT NULL constraints
Setting default empty JSON without validation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.