Bird
0
0

Why does this query fail?

medium📝 Debug Q7 of 15
PostgreSQL - JSON and JSONB
Why does this query fail?
SELECT data->>1 FROM table_name;
Assuming data is a JSON object, not an array.
Adata column is not JSON type
B->> operator cannot be used on JSON objects
CMissing quotes around 1
DUsing numeric index on JSON object instead of key
Step-by-Step Solution
Solution:
  1. Step 1: Understand JSON object vs array access

    JSON objects require string keys, arrays use numeric indexes.
  2. Step 2: Identify misuse of numeric index on object

    Using ->>1 treats 1 as key, but JSON object keys must be strings, so this fails.
  3. Final Answer:

    Using numeric index on JSON object instead of key -> Option D
  4. Quick Check:

    JSON objects need string keys, not numeric indexes [OK]
Quick Trick: Use string keys for objects, numeric indexes for arrays [OK]
Common Mistakes:
  • Using numeric index on JSON object
  • Assuming ->> works only on arrays
  • Not quoting keys for objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes