Disable flag sorting when sorting data

Every time I use jsonify , I get JSON keys sorted alphabetically. I do not want the keys to be sorted. Is it possible to disable sorting in jsonify ?

 from flask import request, jsonify @app.route('/', methods=['POST']) def index(): json_dict = request.get_json() user_id = json_dict['user_id'] permissions = json_dict['permissions'] data = {'user_id': user_id, 'permissions': permissions} return jsonify(data) 
+5
source share
1 answer

Yes, you can change this using the config attribute:

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

However, note that this is explicitly stated in the documentation:

By default, Flask will serialize JSON objects so that the keys are ordered. This is done in order to ensure independence from the hash seed of the dictionary, the return value will correspond not to garbage of external HTTP caches. You can override the default behavior by changing this variable. This is not recommended, but can give you increased performance at the cost of caching.

+11
source

Source: https://habr.com/ru/post/1266436/


All Articles