Bird
0
0

Why does this query fail?

medium📝 Debug Q7 of 15
PostgreSQL - JSON and JSONB
Why does this query fail?
SELECT jsonb_set(data, '{location,city}', '"New York"') FROM table;
Assuming data has no key location.
ABecause the data column is not JSONB type
BBecause '"New York"' is not valid JSON
CBecause jsonb_set cannot update nested keys
DBecause the path includes a missing intermediate key and create_missing is false by default
Step-by-Step Solution
Solution:
  1. Step 1: Check path existence

    The path '{location,city}' requires the key 'location' to exist if create_missing is not set to true.
  2. Step 2: Default behavior

    By default, create_missing is false, so the function fails if intermediate keys are missing.
  3. Final Answer:

    Because the path includes a missing intermediate key and create_missing is false by default -> Option D
  4. Quick Check:

    Missing intermediate keys cause jsonb_set failure [OK]
Quick Trick: Missing path keys cause jsonb_set errors [OK]
Common Mistakes:
  • Assuming jsonb_set auto-creates missing keys
  • Thinking JSON string is invalid here
  • Believing 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