Bird
0
0

Given the JSONB column data with value '{"name": "Alice", "details": {"age": 30}}', what will be the result of this query?

medium📝 query result Q13 of 15
PostgreSQL - JSON and JSONB
Given the JSONB column data with value '{"name": "Alice", "details": {"age": 30}}', what will be the result of this query?
SELECT jsonb_set(data, '{details,age}', '31', false) FROM table;
A{"name": "Alice", "details": {"age": 31}}
B{"name": "Alice", "details": {"age": "31"}}
C{"name": "Alice", "details": {"age": 30}}
D{"name": "Alice", "details": {"age": null}}
Step-by-Step Solution
Solution:
  1. Step 1: Understand jsonb_set parameters

    The function jsonb_set updates the value at path '{details,age}' to '31' (a JSON string) because the new value is given as text, not as a number.
  2. Step 2: Check the effect of the 'create_missing' flag

    The last parameter is false, so it updates existing key without creating new ones. The age key is updated to the string '31', not the number 31.
  3. Final Answer:

    {"name": "Alice", "details": {"age": "31"}} -> Option B
  4. Quick Check:

    jsonb_set updates value as text unless cast [OK]
Quick Trick: jsonb_set inserts text values unless cast to JSONB [OK]
Common Mistakes:
  • Assuming '31' is numeric without casting
  • Expecting no change when create_missing is false
  • Confusing JSON string and number types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes