Bird
0
0

You have a table orders with a JSON column details storing nested JSON like {"customer": {"name": "Alice", "age": 30}}. Which query correctly extracts the customer's name as text?

hard📝 Application Q15 of 15
PostgreSQL - JSON and JSONB
You have a table orders with a JSON column details storing nested JSON like {"customer": {"name": "Alice", "age": 30}}. Which query correctly extracts the customer's name as text?
ASELECT details->'customer'->'name' FROM orders;
BSELECT details->'customer'->>'name' FROM orders;
CSELECT details->>'customer'->>'name' FROM orders;
DSELECT details->>'customer'->'name' FROM orders;
Step-by-Step Solution
Solution:
  1. Step 1: Extract nested JSON object first

    Use -> to get the 'customer' JSON object from 'details'.
  2. Step 2: Extract 'name' as text

    Use ->> on the 'name' key to get its value as text.
  3. Step 3: Analyze other options

    SELECT details->'customer'->'name' FROM orders; returns JSON, not text. SELECT details->>'customer'->>'name' FROM orders; uses ->> on 'customer' which returns text, so next ->> fails. SELECT details->>'customer'->'name' FROM orders; mixes operators incorrectly.
  4. Final Answer:

    SELECT details->'customer'->>'name' FROM orders; -> Option B
  5. Quick Check:

    Use -> then ->> for nested text extraction [OK]
Quick Trick: Use -> for JSON object, then ->> for text key inside it [OK]
Common Mistakes:
  • Using ->> too early breaking JSON chain
  • Mixing operator order incorrectly
  • Expecting JSON output when text is needed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes