Bird
Raised Fist0
PostgreSQLquery~20 mins

JSON vs JSONB differences in PostgreSQL - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JSON vs JSONB Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference in Storage Format
Which statement correctly describes the difference in storage format between JSON and JSONB in PostgreSQL?
ABoth JSON and JSONB store data as plain text but differ in indexing.
BJSON stores data in binary format, while JSONB stores data as plain text.
CJSON stores data as plain text, while JSONB stores data in a binary format.
DBoth JSON and JSONB store data in binary format but differ in compression.
Attempts:
2 left
💡 Hint
Think about how the data is saved internally and how that affects performance.
❓ query_result
intermediate
2:00remaining
Query Result Difference on Duplicate Keys
Given a table with a JSON and a JSONB column both containing the value '{"a":1, "a":2}', what will be the result of selecting these columns?
PostgreSQL
SELECT json_col, jsonb_col FROM test_table;
Ajson_col returns '{"a":1, "a":2}', jsonb_col returns '{"a":2}'
Bjson_col returns '{"a":2}', jsonb_col returns '{"a":1, "a":2}'
CBoth json_col and jsonb_col return '{"a":1, "a":2}'
DBoth json_col and jsonb_col return '{"a":2}'
Attempts:
2 left
💡 Hint
Consider how JSONB handles duplicate keys compared to JSON.
❓ optimization
advanced
2:00remaining
Indexing Performance
Which indexing method is supported only by JSONB and not by JSON in PostgreSQL?
AGIN (Generalized Inverted Index)
BB-tree index
CHash index
DBitmap index
Attempts:
2 left
💡 Hint
Think about which index type helps with fast key/value lookups inside JSON data.
🔧 Debug
advanced
2:00remaining
Error When Using JSON Functions
Which option will cause an error when trying to use the JSONB containment operator '@>' on a JSON column?
PostgreSQL
SELECT * FROM test_table WHERE json_col @> '{"key": "value"}';
AThe query runs successfully and returns matching rows.
BThe query returns no rows but does not raise an error.
CThe query raises a syntax error due to wrong operator usage.
DThe query raises an error: operator does not exist: json @> json.
Attempts:
2 left
💡 Hint
Check if the operator '@>' is supported for the data type used.
🧠 Conceptual
expert
3:00remaining
When to Prefer JSON over JSONB
In which scenario is it better to use JSON instead of JSONB in PostgreSQL?
AWhen you want to avoid storing duplicate keys by default.
BWhen you need to preserve the exact input text including whitespace and key order.
CWhen you want faster query performance and indexing.
DWhen you want to use GIN indexes for complex queries.
Attempts:
2 left
💡 Hint
Think about what JSON preserves that JSONB does not.

Practice

(1/5)
1. Which of the following is a key difference between json and jsonb data types in PostgreSQL?
easy
A. json stores data as plain text, while jsonb stores data in a binary format.
B. jsonb preserves whitespace and formatting, json does not.
C. json supports indexing, jsonb does not.
D. jsonb cannot store nested objects, json can.

Solution

  1. Step 1: Understand storage formats

    json stores JSON data as plain text, preserving the original formatting including whitespace.
  2. Step 2: Understand jsonb storage

    jsonb stores JSON data in a binary format optimized for faster processing and indexing.
  3. Final Answer:

    json stores data as plain text, while jsonb stores data in a binary format. -> Option A
  4. Quick Check:

    Storage format difference = Option A [OK]
Hint: Remember: json = text, jsonb = binary for speed [OK]
Common Mistakes:
  • Thinking jsonb preserves whitespace like json
  • Assuming jsonb cannot index data
  • Believing jsonb cannot store nested objects
2. Which of the following is the correct way to declare a column with jsonb type in PostgreSQL?
easy
A. CREATE TABLE data (info JSONB);
B. CREATE TABLE data (info JSONB[]);
C. CREATE TABLE data (info JSON);
D. CREATE TABLE data (info BINARYJSON);

Solution

  1. Step 1: Review PostgreSQL syntax for JSONB

    The correct data type name is jsonb (case-insensitive), used directly as a column type.
  2. Step 2: Check each option

    Option A uses JSONB correctly. Option A tries to declare an array of jsonb, which is valid but not the simplest declaration. Option A uses JSON type, not jsonb. Option A uses a non-existent type BINARYJSON.
  3. Final Answer:

    CREATE TABLE data (info JSONB); -> Option A
  4. Quick Check:

    Correct jsonb column syntax = Option A [OK]
Hint: Use JSONB directly as column type, no extra keywords [OK]
Common Mistakes:
  • Confusing JSON and JSONB types
  • Using invalid type names like BINARYJSON
  • Adding unnecessary array brackets
3. Given the table items with a json column data storing '{"a":1, "b":2}', what will this query return?
SELECT data @> '{"a":1}'::jsonb FROM items;
medium
A. Error because data is json, but query uses jsonb operator
B. True for rows where data contains key "a" with value 1
C. False for all rows because data is json, not jsonb
D. Returns all rows regardless of content

Solution

  1. Step 1: Understand operator compatibility

    The @> operator is defined for jsonb types, not for json. Using it on a json column causes an error.
  2. Step 2: Analyze the query

    The query tries to use @> on data which is json, but the right side is cast to jsonb. This mismatch causes a type error.
  3. Final Answer:

    Error because data is json, but query uses jsonb operator -> Option A
  4. Quick Check:

    jsonb operator on json column = Error [OK]
Hint: jsonb operators need jsonb columns, not json [OK]
Common Mistakes:
  • Assuming json and jsonb are interchangeable
  • Ignoring type casting errors
  • Expecting operator to work on json type
4. You have a table with a jsonb column but your query is slow when filtering by keys. What is the most likely cause?
medium
A. No GIN or BTREE index on the jsonb column
B. Using json type instead of jsonb
C. The JSON data is stored as plain text
D. The query uses LIKE operator on the jsonb column

Solution

  1. Step 1: Understand indexing for jsonb

    jsonb supports GIN or BTREE indexes to speed up key/value lookups.
  2. Step 2: Identify cause of slow queries

    If no index exists on the jsonb column, queries filtering by keys must scan all rows, causing slowness.
  3. Final Answer:

    No GIN or BTREE index on the jsonb column -> Option A
  4. Quick Check:

    Missing index on jsonb = slow queries [OK]
Hint: Add GIN index on jsonb for fast key lookups [OK]
Common Mistakes:
  • Confusing json and jsonb types
  • Using LIKE on jsonb instead of jsonb operators
  • Ignoring indexing for jsonb columns
5. You want to store JSON data and frequently query nested keys with fast performance. Which approach is best?
hard
A. Store data as json and create expression indexes on keys
B. Store data as jsonb and create a GIN index on the column
C. Store data as text and parse JSON in application code
D. Store data as jsonb but avoid indexes for faster inserts

Solution

  1. Step 1: Choose data type for fast queries

    jsonb is optimized for querying and indexing nested JSON data.
  2. Step 2: Use appropriate indexing

    Creating a GIN index on the jsonb column enables fast key and path lookups.
  3. Final Answer:

    Store data as jsonb and create a GIN index on the column -> Option B
  4. Quick Check:

    jsonb + GIN index = best for fast nested queries [OK]
Hint: Use jsonb with GIN index for fast nested JSON queries [OK]
Common Mistakes:
  • Using json type which is slower for queries
  • Avoiding indexes to speed inserts but hurting queries
  • Parsing JSON outside database causing overhead
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard