Bird
0
0

How can you store a JSON array in a PostgreSQL JSONB column and query the second element of the array?

hard📝 Application Q9 of 15
PostgreSQL - JSON and JSONB
How can you store a JSON array in a PostgreSQL JSONB column and query the second element of the array?
AINSERT INTO data_table (json_col) VALUES ('[10, 20, 30]'); SELECT json_col->>2 FROM data_table;
BINSERT INTO data_table (json_col) VALUES ('{10, 20, 30}'); SELECT json_col->2 FROM data_table;
CINSERT INTO data_table (json_col) VALUES ('[10, 20, 30]'); SELECT json_col->1 FROM data_table;
DINSERT INTO data_table (json_col) VALUES ('(10, 20, 30)'); SELECT json_col->1 FROM data_table;
Step-by-Step Solution
Solution:
  1. Step 1: Insert JSON array syntax

    JSON arrays use square brackets []; INSERT INTO data_table (json_col) VALUES ('[10, 20, 30]'); SELECT json_col->1 FROM data_table; uses correct JSON array syntax.
  2. Step 2: Query second element with zero-based index

    JSON array indexing is zero-based; ->1 extracts second element as JSON.
  3. Final Answer:

    INSERT INTO data_table (json_col) VALUES ('[10, 20, 30]'); SELECT json_col->1 FROM data_table; -> Option C
  4. Quick Check:

    JSON arrays use [] and zero-based indexing [OK]
Quick Trick: JSON arrays use zero-based index with -> operator [OK]
Common Mistakes:
  • Using curly braces {} instead of [] for JSON arrays
  • Using one-based indexing for JSON arrays
  • Using ->> operator expecting JSON value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes