beka beka - 5 months ago 59
Python Question

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


return jsonify(result=my_list)

and it gets

"result": [


Use the standard-library json module instead, and set the ensure_ascii keyword parameter to False when encoding:

>>> data = u'\u10e2\u10d4\u10e1\u10e2'
>>> import json
>>> json.dumps(data)
>>> json.dumps(data, ensure_ascii=False)
>>> print json.dumps(data, ensure_ascii=False)
>>> json.dumps(data, ensure_ascii=False).encode('utf8')

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