Bird
0
0

Which of the following is the correct syntax to insert JSON data into a table named orders with a jsonb column info?

easy📝 Syntax Q3 of 15
PostgreSQL - JSON and JSONB
Which of the following is the correct syntax to insert JSON data into a table named orders with a jsonb column info?
AINSERT INTO orders (info) VALUES ('{item: "book", qty: 3}');
BINSERT INTO orders (info) VALUES (to_jsonb('{"item": "book", "qty": 3}'));
CINSERT INTO orders (info) VALUES (jsonb '{"item": "book", "qty": 3}');
DINSERT INTO orders (info) VALUES ('{"item": "book", "qty": 3}'::jsonb);
Step-by-Step Solution
Solution:
  1. Step 1: Understand JSONB insertion syntax

    To insert JSONB data, you can cast a string literal to jsonb using ::jsonb.
  2. Step 2: Evaluate each option

    INSERT INTO orders (info) VALUES ('{item: "book", qty: 3}'); uses invalid JSON (unquoted keys), causing error. INSERT INTO orders (info) VALUES (jsonb '{"item": "book", "qty": 3}'); uses invalid syntax. INSERT INTO orders (info) VALUES (to_jsonb('{"item": "book", "qty": 3}')); tries to use to_jsonb() on a string literal, which is incorrect usage.
  3. Final Answer:

    INSERT INTO orders (info) VALUES ('{"item": "book", "qty": 3}'::jsonb); -> Option D
  4. Quick Check:

    Insert JSONB with cast = correct syntax [OK]
Quick Trick: Cast JSON string to jsonb using ::jsonb when inserting [OK]
Common Mistakes:
  • Inserting JSON as plain string
  • Using invalid function calls
  • Missing cast to jsonb

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes