29

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?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
eugene
  • 39,839
  • 68
  • 255
  • 489

1 Answers1

47

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.

ecatmur
  • 152,476
  • 27
  • 293
  • 366