BinlogStreamReader creating binary key names for json objects

43 views
Skip to first unread message

Chris Lomeli

unread,
Dec 30, 2023, 8:58:06 PM12/30/23
to Python MySQL replication
Hi.  this is an awesome project, but I need to be able to convert rows to json, and if the mysql column is also json then I get binary key names like b'1', b'2' etc.   And json.dumps() cannot convert them.   It's easy to see where to change (parse_json_object_or_array), but it would probably break something else I don't know about.   Any advice appreciated, but I'm assuming is the way iit is for a reason and just casting these keys to str() might break something.

Aaron Close

unread,
Jan 28, 2024, 8:05:33 PM1/28/24
to Python MySQL replication
I put this behind a try except around my json.dumps

def bytes_to_str(input):
if isinstance(input, bytes):
return input.decode('utf-8')
elif isinstance(input, Decimal):
return float(input)
elif isinstance(input,datetime):
return input.isoformat()
elif isinstance(input, dict):
return {bytes_to_str(key): bytes_to_str(value) for key, value in input.items()}
elif isinstance(input, list):
return [bytes_to_str(element) for element in input]
else:
return input

Reply all
Reply to author
Forward
0 new messages