0

I am running the below code to fetch the data from an apache kafka consumer in python.

from kafka import KafkaConsumer
import json

consumer = KafkaConsumer("DataEngFuelData", bootstrap_servers=['localhost:9092'], group_id=None,
                         auto_offset_reset='earliest')
try:
    for msg in consumer:
        decoded_msg = msg.value.decode('utf-8-sig')
        decoded_msgtmp = json.loads(decoded_msg)
        print(decoded_msgtmp)
        dictmsg = decoded_msgtmp['stations']
        print(dictmsg)

except Exception as e:      
    print(e)

However, when the program executes the json.loads() function it gives me the error

Expecting value: line 2 column 11 (char 12).
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Have you looked at the value in decoded_msg to see if it is malformed? – Frank Merrow Feb 02 '20 at 21:02
  • Yes it gives me string object. "Stations":{ "Id": "2645B215-8233-49Dc-85F4-B389B284F624", "Name": "Aral Tankstelle", "Brand": "Aral", "Street": "Bergheimer Stra\U00Dfe", "Place": "Heidelberg" } Even though I have decoded it it is giving me strings like "Street": "Bergheimer Stra\U00Dfe" in the output. can this cause any issue? – Vinayak Parab Feb 03 '20 at 10:01
  • How is data produced into the topic? Do you trust its always valid JSON? Why are you using `utf-8-sig`? And why aren't you passing a value deserailizer into the consumer class? – OneCricketeer Feb 03 '20 at 15:06
  • I got it figured out. Thanks for your help – Vinayak Parab Feb 04 '20 at 16:32

0 Answers0