Dumping a string that contains unicode characters as json produces weird unicode escape sequences:
text = "⌂⚘いの法嫁" print(text) # output: ⌂⚘いの法嫁 import json json_text = json.dumps(text) print(json_text) # output: "\u2302\u2698\u3044\u306e\u6cd5\u5ac1"
I'd like to get this output instead:
"⌂⚘いの法嫁"
How can I dump unicode characters as characters instead of escape sequences?
JSON data always uses the Unicode character set.
json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.
loads() takes in a string and returns a json object. json. dumps() takes in a json object and returns a string.
Call json.dumps
with ensure_ascii=False
:
json_string = json.dumps(json_dict, ensure_ascii=False)
On Python 2, the return value will be unicode
instead of str
, so you might want to encode
it before doing anything else with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With