Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python JSON loads/dumps breaks Unicode?

Tags:

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?

like image 636
eugene Avatar asked Aug 01 '12 12:08

eugene


People also ask

Can JSON contain Unicode?

JSON data always uses the Unicode character set.

What is the difference between JSON dump and JSON dumps?

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.

What is the difference between JSON dumps and JSON loads in Python?

loads() takes in a string and returns a json object. json. dumps() takes in a json object and returns a string.


1 Answers

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.

like image 53
ecatmur Avatar answered Sep 18 '22 02:09

ecatmur