Search code examples
jsonpython-3.xdictionaryreversesimplejson

How to reverse order nested dictionary in python3?


Apologies if similar question was already asked though I've not been able to find answers after comprehensive googling. I've been struggling with exporting following dictionary into a JSON file with keys ordered in descending order. The file needs to be readable outside Python, thus pickle can't be used.

I'm using Python3.7 and I've tried using simplejson and json libraries to dump the nested dict into a json file, though have been unsuccessful. Pickle seemed to work, though that exports a binary file which is unreadable outside Python. Dict is called 'existing'.

with open('Entries_2.json', 'w') as outfile:
    simplejson.dump(existing,outfile,item_sort_key=simplejson.simple_first)
outfile.close()

with open('Entries.json', 'w') as outfile:
    json.dump(existing, outfile, sort_keys=True, indent=4)
outfile.close()

Both of the above methods produce same result:

{
    "Entries": {
        "2019/01/23": {
            "Result-9595905890": {
                "count": 4,
                "time": "2019/01/23 03:32:32"
            }
        },
        "2019/01/24": {
            "Result-9607169713": {
                "count": 21,
                "time": "2019/01/24 03:31:34"
            },
            "Result-9611777668": {
                "count": 23,
                "time": "2019/01/24 12:58:49"
            }
        },
        "2019/01/25": {
            "Result-9618433556": {
                "count": 21,
                "time": "2019/01/25 03:31:27"
            }
        }
    }
}

Though reverse=True option doesn't work in either dump option. What I need is:

{
    "Entries": {
        "2019/01/25": {
            "Result-9618433556": {
                "count": 21,
                "time": "2019/01/25 03:31:27"
            }
        },
        "2019/01/24": {
            "Result-9607169713": {
                "count": 21,
                "time": "2019/01/24 03:31:34"
            },
            "Result-9611777668": {
                "count": 23,
                "time": "2019/01/24 12:58:49"
            }
        },
        "2019/01/23": {
            "Result-9595905890": {
                "count": 4,
                "time": "2019/01/23 03:32:32"
            }
        }
    }
}

Has anyone come across similar struggles?


Solution

  • Try looking at collections.OrderedDict

    import json, collections
    
    existing = {
        "Entries": {
            "2019/01/23": {
                "Result-9595905890": {
                    "count": 4,
                    "time": "2019/01/23 03:32:32"
                }
            },
            "2019/01/24": {
                "Result-9607169713": {
                    "count": 21,
                    "time": "2019/01/24 03:31:34"
                },
                "Result-9611777668": {
                    "count": 23,
                    "time": "2019/01/24 12:58:49"
                }
            },
            "2019/01/25": {
                "Result-9618433556": {
                    "count": 21,
                    "time": "2019/01/25 03:31:27"
                }
            }
        }
    }
    
    new_entries = collections.OrderedDict(reversed(sorted(existing['Entries'].items()))) # if you want reversed sorted
    existing['Entries'] = new_entries
    
    with open('Entries.json', 'w') as outfile:
        json.dump(existing, outfile, indent=4)
    

    BTW, when you do with open('Entries.json', 'w') as outfile: it will automatically close outfile after the with statement is completed, so you don't need to explicitly close it.

    Output:

    {
        "Entries": {
            "2019/01/25": {
                "Result-9618433556": {
                    "count": 21,
                    "time": "2019/01/25 03:31:27"
                }
            },
            "2019/01/24": {
                "Result-9607169713": {
                    "count": 21,
                    "time": "2019/01/24 03:31:34"
                },
                "Result-9611777668": {
                    "count": 23,
                    "time": "2019/01/24 12:58:49"
                }
            },
            "2019/01/23": {
                "Result-9595905890": {
                    "count": 4,
                    "time": "2019/01/23 03:32:32"
                }
            }
        }
    }