JSON_AS_ASCII no longer works after upgrading Flask

By default, Flask serialize objects to ASCII-encoded JSON. In the past, I use the app.config['JSON_AS_ASCII'] = False to disable this behavior.

Recently, I upgraded my Flask package to the latest (3.x), and the JSON_AS_ASCII configuration no longer works.

What happened

On the official document, I found this:

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.

It means that app.config['JSON_AS_ASCII'] no longer works since version 2.3.

Solution

Replace outdated code app.config['JSON_AS_ASCII'] = False with this:

app.json.ensure_ascii = False
Posted on 2023-12-01