0
0
PostgreSQLquery~10 mins

Inserting JSON data in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a JSON object into the table.

PostgreSQL
INSERT INTO products (data) VALUES ('[1]');
Drag options to blanks, or click blank then click option'
A'name': 'Book', 'price': 12.99
Bname: Book, price: 12.99
C["name", "Book"]
D{"name": "Book", "price": 12.99}
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for keys or string values inside JSON.
Using array brackets instead of curly braces for JSON objects.
2fill in blank
medium

Complete the code to insert JSON data using the jsonb type.

PostgreSQL
INSERT INTO orders (order_info) VALUES ([1]);
Drag options to blanks, or click blank then click option'
A'{id: 101, status: "shipped"}'::jsonb
B'{"id": 101, "status": shipped}'::jsonb
C'{"id": 101, "status": "shipped"}'::jsonb
D'{"id": 101, "status": "shipped"}'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to cast the string to jsonb.
Missing double quotes around string values.
3fill in blank
hard

Fix the error in the INSERT statement for JSON data.

PostgreSQL
INSERT INTO users (profile) VALUES ('[1]'::json);
Drag options to blanks, or click blank then click option'
A'{"age": 30, "city": "NY"}'
C{"age": 30, city: "NY"}
D'{age: 30, city: NY}'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes inside JSON instead of double quotes.
Not enclosing the JSON string in single quotes in SQL.
4fill in blank
hard

Fill both blanks to insert JSON data with a nested object.

PostgreSQL
INSERT INTO events (details) VALUES ('[1]'::[2]);
Drag options to blanks, or click blank then click option'
A{"event": "login", "user": {"id": 5}}
Bjson
Cjsonb
D{"event": login, "user": {id: 5}}
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys or string values inside JSON.
Casting to json instead of jsonb when jsonb is preferred.
5fill in blank
hard

Fill all three blanks to insert JSON data with an array and cast it properly.

PostgreSQL
INSERT INTO catalog (info) VALUES ('[1]'::[2]); -- array has [3] items
Drag options to blanks, or click blank then click option'
A{"products": ["pen", "notebook", "eraser"]}
Bjsonb
C3
D{"products": [pen, notebook, eraser]}
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting string values inside the JSON array.
Casting to json instead of jsonb.
Incorrectly counting the number of items in the array.