Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode values in strings are escaped when dumping to JSON in Python

For example:

>>> print json.dumps('růže')
"r\u016f\u017ee"

(Of course, in the real program it's not just a single string, and it also appears like this in the file, when using json.dump()) I'd like it to output simply "růže" as well, how to do that?

like image 902
Theon144 Avatar asked Jun 02 '12 19:06

Theon144


People also ask

Is Unicode valid in JSON?

JSON data always uses the Unicode character set. In this respect, JSON data is simpler to use than XML data. This is an important part of the JSON Data Interchange Format (RFC 4627).

What is Unicode escape Python?

In Python source code, Unicode literals are written as strings prefixed with the 'u' or 'U' character: u'abcdefghijk'. Specific code points can be written using the \u escape sequence, which is followed by four hex digits giving the code point. The \U escape sequence is similar, but expects 8 hex digits, not 4.

Does JSON dumps return a string?

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

Is JSON always UTF-8?

JSON in HTTP are always encoded in UTF-8. Responses are parsed correctly when server writes content type header like application/json; charset=utf-8 . However, many servers (like Play framework itself) uses application/json without charset. In Play 2.6, that responses are parsed in ISO-8859-1 charset.


1 Answers

Pass the ensure_ascii=False argument to json.dumps:

>>> print json.dumps('růže', ensure_ascii=False)
"růže"
like image 82
BrenBarn Avatar answered Sep 23 '22 02:09

BrenBarn