0

I am trying to encode a python dictionary as JSON and make sure that the resulting string is utf-8 encoded.

In my first try my code looked like the following.

import json
from json import JSONEncoder


class MyEncoder(JSONEncoder):
    def default(self, o):
        return o.__dict__


draft = json.dumps(myObject, sort_keys=True, indent=2, cls=MyEncoder, ensure_ascii=False)
draft = draft.encode('utf8')
print("DEBUG: CLI will send the following JSON: {}".format(draft))

Which results in

DEBUG: CLI will send the following JSON: b'{\n  "a": "ABC",\n  "d": "Lorem ipsum dolor sit amet, invidunt \xc3\xbct",\n  "t": "SingleApp"\n}'

I expected (Note the 'ü' and the whitespace characters):

DEBUG: CLI will send the following JSON: {
  "a": "ABC",
  "d": "Lorem ipsum dolor sit amet, invidunt üt",
  "t": "SingleApp"
}

In my second try I left out draft = draft.encode('utf8') and the result looks as expected but can I trust it to always work like that? Is it going to be encoded differently on a differently configured machine? "Works for me" doesn'nt work for me.

Human
  • 726
  • 8
  • 27
  • Does this answer your question? [Saving utf-8 texts with json.dumps as UTF8, not as \u escape sequence](https://stackoverflow.com/questions/18337407/saving-utf-8-texts-with-json-dumps-as-utf8-not-as-u-escape-sequence) – Paweł Rubin Apr 01 '22 at 10:00
  • Thats how I got to my first try in the first place, apparently I forgot to use decode() in the print statement. So are you saying that encoding and decoding is the only way to make sure? – Human Apr 01 '22 at 11:09

0 Answers0