I am sending json objects from kafka producer to kafka consumer but I am gettin the the error:
TypeError: list indices must be integers or slices, not dict.
Below is my code for kafka producer:
jsonFilePath = "data.json"
data = None
with open(jsonFilePath) as f:
data = json.load(f)
print(type(data))
print(len(data))
count = 0
for record in data:
# print(str(data[record]))
# print(data[record])
producer.send('testTopic', value=data[record])
count = count + 1
And my json objects looks like:
{
"Order Number": "CA-2014-103800",
"Order Status": "Completed",
"Order Date": "03/01/2014",
"First Name (Billing)": "Saima",
"Last Name (Billing)": "Nasir",
"Company (Billing)": "",
"Address 1&2 (Billing)": "",
"City (Billing)": "",
"State Code (Billing)": "PB",
"Postcode (Billing)": 54810,
"Country Code (Billing)": "PK",
"Email (Billing)": "",
"First Name (Shipping)": "Saima",
"Last Name (Shipping)": "Nasir",
"Address 1&2 (Shipping)": "",
"City (Shipping)": "Lahore",
"State Code (Shipping)": "PB",
"Postcode (Shipping)": 54810,
"Country Code (Shipping)": "PK",
"Shipping Method Title": "Free shipping",
"Payment Method Title": "Cash on delivery",
"Cart Discount Amount": 0,
"Order Subtotal Amount": 0,
"Order Shipping Amount": 0,
"Order Refund Amount": 0,
"Order Total Amount": 16.448,
"Order Total Tax Amount": 0,
"Category": "Kids",
"Item # #1": 1,
"SKU #1": "",
"Item Name #1": "Ladies Shoes - Navy, 40",
"Quantity #1": 2,
"Item Cost #1": 0,
"Coupon Code": "",
"Discount Amount": 0.2,
"Discount Amount Tax": "",
"Profit": 5.5512
}
Inside your for
loop, record
is not the index, but the actual value instead.
Therefore, instead of
for record in data:
producer.send('testTopic', value=data[record])
try the following:
for record in data:
producer.send('testTopic', value=record])