#!/usr/bin/env python3 import sys import json import csv # Read CSV header header = next(sys.stdin).strip().split(',') header = [h.strip() for h in header] # Read and process each row for line in sys.stdin: line = line.strip() if not line: continue try: values = next(csv.reader([line])) if len(values) != len(header): continue record = dict(zip(header, values)) # Convert data types record["transaction_id"] = int(record["transaction_id"]) record["quantity"] = int(record["quantity"]) record["price"] = float(record["price"]) # Output JSON print(json.dumps(record)) except (ValueError, StopIteration): continue
transaction_id,user_id,product_id,quantity,price,timestamp 1001,user_A,prod_X,2,50.00,2023-01-01T12:00:00Z 1002,user_B,prod_Y,1,120.50,2023-01-01T12:05:00Z 1003,user_C,prod_Z,3,75.00,2023-01-01T12:10:00Z 1004,user_A,prod_Y,2,120.50,2023-01-01T12:15:00Z 1005,user_B,prod_X,1,50.00,2023-01-01T12:20:00Z
{"transaction_id": 1001, "user_id": "user_A", "product_id": "prod_X", "quantity": 2, "price": 50.0, "timestamp": "2023-01-01T12:00:00Z"}
{"transaction_id": 1002, "user_id": "user_B", "product_id": "prod_Y", "quantity": 1, "price": 120.5, "timestamp": "2023-01-01T12:05:00Z"}
{"transaction_id": 1003, "user_id": "user_C", "product_id": "prod_Z", "quantity": 3, "price": 75.0, "timestamp": "2023-01-01T12:10:00Z"}
{"transaction_id": 1004, "user_id": "user_A", "product_id": "prod_Y", "quantity": 2, "price": 120.5, "timestamp": "2023-01-01T12:15:00Z"}
{"transaction_id": 1005, "user_id": "user_B", "product_id": "prod_X", "quantity": 1, "price": 50.0, "timestamp": "2023-01-01T12:20:00Z"}