Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python json.dumps() doesn't encode emojis properly [duplicate]

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

like image 534
Thomas Avatar asked Aug 28 '26 18:08

Thomas


1 Answers

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

like image 117
Amadan Avatar answered Aug 31 '26 09:08

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!