Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Flask's jsonify displays é as é

jsonify('é') doesn't print what I expect. Instead of é, I see é.

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

@app.route('/')
def test():
    return jsonify('é')

The encoding of the script is UTF-8. UTF-8 JSON encoding is supposed to be activated by JSON_AS_ASCII = False.

like image 267
Rodolphe LAMPE Avatar asked Sep 11 '26 12:09

Rodolphe LAMPE


1 Answers

You're viewing the representation of the dumped data. Since you've disabled JSON_AS_ASCII, you get two UTF-8 bytes rather than an ASCII-compatible Unicode escape. JSON is still UTF-8, regardless of which representation you choose, but it is typically safer to stick with the default.

Whatever you're using to view the data is misinterpreting the bytes as Latin-1, not UTF-8. Tell whatever you're viewing the data with that it's UTF-8, and it will look correct. Load the data from JSON and you will see that it is still correct.

from flask import Flask, jsonify, json

app = Flask('example')
app.config['JSON_AS_ASCII'] = True  # default

with app.app_context():
    print(jsonify('é').data)  # b'"\\u00e9"\n', Unicode escape

app.config['JSON_AS_ASCII'] = False

with app.app_context():
    print(jsonify('é').data)  # b'"\xc3\xa9"\n', UTF-8 bytes

# you're viewing the bytes as Latin-1
print(b'\xc3\xa9'.decode('latin1'))  # é

# but it's UTF-8
print(b'\xc3\xa9'.decode('utf8'))  # é

# JSON is always UTF-8
print(json.loads(b'"\\u00e9"\n')  # é
print(json.loads(b'"\xc3\xa9"\n')  # é
like image 156
davidism Avatar answered Sep 13 '26 02:09

davidism