I am trying to dump a json content like:
foo = simplejson.dumps(data)
But I am seeing the following error:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd6 in position 33: invalid continuation byte
How should I en/decode it properly ?
asked Sep 27, 2015 at 0:36
frazman
33.5k82 gold badges194 silver badges283 bronze badges
1 Answer 1
Your data contains str objects that contain non-UTF-8 bytes. All text in JSON is Unicode, so str values are decoded to Unicode assuming UTF-8.
If that does not apply to all text in your data, you either need to decode it to Unicode before dumping to JSON, or you need to tell the dumps() function what codec to decode bytestrings with:
foo = simplejson.dumps(data, encoding='<codec for bytestrings in data>')
answered Sep 27, 2015 at 0:38
Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
datathat reproduces your problem. Please provide us with a minimal reproducible example if you want to get specific help.