← All Assignments
JSON to SQLite Pipeline
Problem Statement
Write a Python script that reads data from a JSON file `orders.json`, transforms it, and loads it into a SQLite database. The JSON has this structure: [ {"order_id": 1, "customer": "Alice", "items": ["Laptop", "Mouse"], "total": 51500}, {"order_id": 2, "customer": "Bob", "items": ["Keyboard"], "total": 3000} ] Your pipeline should: 1. Read and parse the JSON file 2. Create a SQLite database `orders.db` with an `orders` table 3. Transform: convert the items list to a comma-separated string 4. Insert all records into the table 5. Query and print all rows to verify the load 6. Print total records inserted
Sample Data
orders.json:
[
{"order_id": 1, "customer": "Alice", "items": ["Laptop", "Mouse"], "total": 51500},
{"order_id": 2, "customer": "Bob", "items": ["Keyboard"], "total": 3000},
{"order_id": 3, "customer": "Carol", "items": ["Monitor", "HDMI"], "total": 22000}
]
Expected Output
orders table: (1, 'Alice', 'Laptop,Mouse', 51500)
(2, 'Bob', 'Keyboard', 3000)
(3, 'Carol', 'Monitor,HDMI', 22000)
Total records inserted: 3