Bird
0
0

You have a table configs with a jsonb column settings. You want to insert a JSON object with a nested array. Which query correctly inserts this data?

hard📝 Application Q9 of 15
PostgreSQL - JSON and JSONB
You have a table configs with a jsonb column settings. You want to insert a JSON object with a nested array. Which query correctly inserts this data?
JSON: {"theme": "dark", "features": ["search", "export"]}
AINSERT INTO configs (settings) VALUES ('{"theme": "dark", "features": ["search", "export"]}'::jsonb);
BINSERT INTO configs (settings) VALUES (to_jsonb('{"theme": "dark", "features": [search, export]}'));
CINSERT INTO configs (settings) VALUES ('{"theme": dark, "features": ["search", "export"]}');
DINSERT INTO configs (settings) VALUES ('{"theme": "dark", "features": "search, export"}'::jsonb);
Step-by-Step Solution
Solution:
  1. Step 1: Validate JSON syntax

    INSERT INTO configs (settings) VALUES ('{"theme": dark, "features": ["search", "export"]}'::jsonb); has incorrect JSON with unquoted "dark" string. INSERT INTO configs (settings) VALUES (to_jsonb('{"theme": "dark", "features": [search, export]}')); misses quotes around array elements. INSERT INTO configs (settings) VALUES ('{"theme": "dark", "features": ["search", "export"]}'::jsonb); has correct JSON with quoted strings and array syntax. INSERT INTO configs (settings) VALUES ('{"theme": "dark", "features": "search, export"}'::jsonb); stores array as a string, not an array.
  2. Step 2: Confirm correct insertion

    INSERT INTO configs (settings) VALUES ('{"theme": "dark", "features": ["search", "export"]}'::jsonb); casts the valid JSON string to jsonb correctly.
  3. Final Answer:

    INSERT INTO configs (settings) VALUES ('{"theme": "dark", "features": ["search", "export"]}'::jsonb); -> Option A
  4. Quick Check:

    Insert nested JSON arrays with proper quotes and cast [OK]
Quick Trick: Always quote strings inside JSON arrays and cast to jsonb [OK]
Common Mistakes:
  • Missing quotes in JSON strings
  • Storing arrays as strings
  • Invalid JSON syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes