Bird
0
0

You want to insert multiple JSON objects into a table events with a jsonb column payload. Which query correctly inserts two JSON objects in one statement?

hard📝 Application Q15 of 15
PostgreSQL - JSON and JSONB
You want to insert multiple JSON objects into a table events with a jsonb column payload. Which query correctly inserts two JSON objects in one statement?
AINSERT INTO events (payload) VALUES (jsonb '{"type": "click", "x": 10}'), (jsonb '{"type": "scroll", "y": 20}');
BINSERT INTO events (payload) VALUES '[{"type": "click", "x": 10}, {"type": "scroll", "y": 20}]';
CINSERT INTO events (payload) VALUES (jsonb '[{"type": "click", "x": 10}, {"type": "scroll", "y": 20}]');
DINSERT INTO events (payload) VALUES (jsonb '{type: click, x: 10}'), (jsonb '{type: scroll, y: 20}');
Step-by-Step Solution
Solution:
  1. Step 1: Insert multiple rows with separate JSON objects

    Use multiple VALUES tuples, each with a valid JSON string cast to jsonb.
  2. Step 2: Ensure JSON keys and strings are double-quoted

    Each JSON object must have keys and string values in double quotes inside single quotes.
  3. Final Answer:

    INSERT INTO events (payload) VALUES (jsonb '{"type": "click", "x": 10}'), (jsonb '{"type": "scroll", "y": 20}'); -> Option A
  4. Quick Check:

    Multiple rows with valid JSON objects = A [OK]
Quick Trick: Use multiple VALUES tuples with jsonb cast for multiple JSON inserts [OK]
Common Mistakes:
  • Trying to insert JSON array as one JSON object
  • Using invalid JSON without quotes
  • Not casting JSON string to jsonb when required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes