how to keep order of sorted dictionary passed to jsonify() function?

This page summarizes the projects mentioned and recommended in the original post on

Our great sponsors
  • Scout APM - Less time debugging, more time building
  • SonarLint - Deliver Cleaner and Safer Code - Right in Your IDE of Choice!
  • SaaSHub - Software Alternatives and Reviews
  • Flask

    The Python micro framework for building web applications.

    from collections import OrderedDict>>>>>> json_data = {... "11": {... "j_id": "out",... },... "aa": {... "j_id": "in",... },... "bb": {... "j_id": "out",... },... }>>>>>> jkeys=json_data.keys()>>> sorted_json = sorted(jkeys, key=lambda x: json_data[x]['j_id'], reverse=False)>>>>>> new_sorted=OrderedDict()>>> for rec in sorted_json:... new_sorted[rec]=json_data[rec]...>>> print('sort dict: {}'.format(new_sorted))sort dict: OrderedDict([('aa', {'j_id': 'in'}), ('11', {'j_id': 'out'}), ('bb', {'j_id': 'out'})]) ``` Unfortunately, when I pass this to jsonify() function and then for example console.log() output of flask data, the orderd becomes like that: 11, aa, bb. Additionally to that, I've done some research and found this stackoverflow answer, leading to some good documentation notes which clearly states that setting JSON_SORT_KEYS to False is not recommended way. Then I checked this github issue and it seems that problem is not fully resolved in flask.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts