4

Why does json.dumps() encode emojis into unicode? See code and output below:

import json
obj = {"key": "hello "}
print(obj)

{'key': 'hello '}

print(json.dumps(obj))

'{"key": "hello \ud83d\ude00"}'

I have tried print(json.dumps(obj)).encode('utf-8') and some variants (.decode()...) but it didn't change the output much. Im working on Python 3.6.1

Thomas
  • 8,306
  • 8
  • 53
  • 92

1 Answers1

12
print(json.dumps(obj, ensure_ascii=False))

However, the ASCII variant is more portable, since you are almost guaranteed you won't have encoding problems. Docs

Amadan
  • 191,408
  • 23
  • 240
  • 301