Bird
0
0

You want to update a nested key in a JSONB column but your query

medium📝 Debug Q14 of 15
PostgreSQL - JSON and JSONB
You want to update a nested key in a JSONB column but your query
UPDATE table SET data = jsonb_set(data, '{info,score}', 100);

throws an error. What is the likely cause?
AThe new value 100 is not cast to JSONB, causing a type error.
BThe path '{info,score}' is invalid syntax and must be a string.
Cjsonb_set cannot update nested keys, only top-level keys.
DThe data column is not JSONB type.
Step-by-Step Solution
Solution:
  1. Step 1: Check the new value type in jsonb_set

    The third argument must be JSONB type. Passing 100 as an integer without casting causes a type error.
  2. Step 2: Verify other options

    The path '{info,score}' is invalid syntax and must be a string. is incorrect because the path syntax is valid. jsonb_set cannot update nested keys, only top-level keys. is false; jsonb_set supports nested keys. The data column is not JSONB type. might cause error but question implies the error is from the query syntax.
  3. Final Answer:

    The new value 100 is not cast to JSONB, causing a type error. -> Option A
  4. Quick Check:

    jsonb_set value must be JSONB type [OK]
Quick Trick: Cast new value to JSONB to avoid type errors [OK]
Common Mistakes:
  • Passing plain integers without casting
  • Misunderstanding path syntax
  • Assuming jsonb_set can't update nested keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes