Beka Tomashvili
Beka Tomashvili

Reputation: 2301

python jsonify dictionary in utf-8

I want to get json data into utf-8

I have a list my_list = []

and then many appends unicode values to the list like this

my_list.append(u'ტესტ')

return jsonify(result=my_list)

and it gets

{
"result": [
"\u10e2\u10d4\u10e1\u10e2",
"\u10e2\u10dd\u10db\u10d0\u10e8\u10d5\u10d8\u10da\u10d8"
]
}

Upvotes: 60

Views: 54045

Answers (6)

Cui Qing
Cui Qing

Reputation: 207

The 'JSON_AS_ASCII' config key is deprecated and will be removed in Flask 2.3. Use app.json.ensure_ascii=False instead.

Upvotes: 4

Kirill Pukhov
Kirill Pukhov

Reputation: 91

Changed in version 2.3: JSON_AS_ASCII, JSON_SORT_KEYS, JSONIFY_MIMETYPE, and JSONIFY_PRETTYPRINT_REGULAR were removed. The default app.json provider has equivalent attributes instead.

Now we can change this behaviour like this:

from flask import json

json.provider.DefaultJSONProvider.ensure_ascii = False

Upvotes: 7

robus gauli
robus gauli

Reputation: 391

If you still want to user flask's json and ensure the utf-8 encoding then you can do something like this:

from flask import json,Response
@app.route("/")
def hello():
    my_list = []
    my_list.append(u'ტესტ')
    data = { "result" : my_list}
    json_string = json.dumps(data,ensure_ascii = False)
    #creating a Response object to set the content type and the encoding
    response = Response(json_string,content_type="application/json; charset=utf-8" )
    return response

#I hope this helps

Upvotes: 9

Martijn Pieters
Martijn Pieters

Reputation: 1122142

Use the standard-library json module instead, and set the ensure_ascii keyword parameter to False when encoding, or do the same with flask.json.dumps():

>>> data = u'\u10e2\u10d4\u10e1\u10e2'
>>> import json
>>> json.dumps(data)
'"\\u10e2\\u10d4\\u10e1\\u10e2"'
>>> json.dumps(data, ensure_ascii=False)
u'"\u10e2\u10d4\u10e1\u10e2"'
>>> print json.dumps(data, ensure_ascii=False)
"ტესტ"
>>> json.dumps(data, ensure_ascii=False).encode('utf8')
'"\xe1\x83\xa2\xe1\x83\x94\xe1\x83\xa1\xe1\x83\xa2"'

Note that you still need to explicitly encode the result to UTF8 because the dumps() function returns a unicode object in that case.

You can make this the default (and use jsonify() again) by setting JSON_AS_ASCII to False in your Flask app config.

WARNING: do not include untrusted data in JSON that is not ASCII-safe, and then interpolate into a HTML template or use in a JSONP API, as you can cause syntax errors or open a cross-site scripting vulnerability this way. That's because JSON is not a strict subset of Javascript, and when disabling ASCII-safe encoding the U+2028 and U+2029 separators will not be escaped to \u2028 and \u2029 sequences.

Upvotes: 34

Ture Friese
Ture Friese

Reputation: 386

In my case the above solution was not sufficient. (Running flask on the GCP App Engine flexible environment). I ended up doing:

json_str = json.dumps(myDict, ensure_ascii = False, indent=4, sort_keys=True)
encoding = chardet.detect(json_str)['encoding']
json_unicode = json_str.decode(encoding)
json_utf8 = json_unicode.encode('utf-8')
response = make_response(json_utf8)
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['mimetype'] = 'application/json'
response.status_code = status

Upvotes: 0

Osvaldo Colina
Osvaldo Colina

Reputation: 1364

Use the following config to add UTF-8 support:

app.config['JSON_AS_ASCII'] = False

Upvotes: 111

Related Questions