Concept Flow - Inserting JSON data
Start
Prepare JSON data
Write INSERT statement with JSON
Execute INSERT
Data stored in JSON column
End
This flow shows how JSON data is prepared and inserted into a PostgreSQL table with a JSON column.
CREATE TABLE users ( id SERIAL PRIMARY KEY, info JSON ); INSERT INTO users (info) VALUES ('{"name": "Alice", "age": 30}');
| Step | Action | Input/Query | Result/State |
|---|---|---|---|
| 1 | Create table | CREATE TABLE users (id SERIAL PRIMARY KEY, info JSON); | Table 'users' created with 'info' column of type JSON |
| 2 | Prepare JSON data | {"name": "Alice", "age": 30} | JSON string ready to insert |
| 3 | Insert JSON data | INSERT INTO users (info) VALUES ('{"name": "Alice", "age": 30}'); | One row inserted with JSON data in 'info' column |
| 4 | Verify insert | SELECT * FROM users; | Returns: id=1, info={"name": "Alice", "age": 30} |
| 5 | End | - | Data stored successfully, execution complete |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| users table | Does not exist | Exists with JSON column | Contains 1 row with JSON data | Contains 1 row with JSON data |
Inserting JSON data in PostgreSQL:
- Create a table with a JSON or JSONB column.
- Prepare JSON data as a string.
- Use INSERT INTO table (json_column) VALUES ('json_string');
- JSON must be enclosed in single quotes.
- Verify insertion with SELECT query.json column named data?users with a jsonb column info?jsonb '...' .products with a json column details, what will be the result of this query?INSERT INTO products (details) VALUES ('{"color": "red", "size": "M"}');
SELECT details->>'color' FROM products WHERE details->>'size' = 'M';INSERT INTO orders (info) VALUES ('{customer: "Alice", total: 50}');events with a jsonb column payload. Which query correctly inserts two JSON objects in one statement?Pass the test and this pattern moves unknown