0

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
2
  • I've given you a generic answer, because you neglected to give us sample data that reproduces your problem. Please provide us with a minimal reproducible example if you want to get specific help. Commented Sep 27, 2015 at 0:39
  • Check this answer for a clue: stackoverflow.com/questions/5552555/… Commented Sep 27, 2015 at 0:40

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.