12

Sorting the Keys of a Python JSON Object

 3 years ago
source link: https://fuzzyblog.io/blog/python/2020/06/11/sorting-the-keys-of-a-python-json-object.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Sorting the Keys of a Python JSON Object

Jun 11, 2020

IMG_3735.jpeg

I recently had to look at a complex JSON structure in Python. The way that I wanted to do this was to look at a sorted list of the keys. Here's the core of what I came up with:

keys = my_json.keys()
keys = list(keys)
keys.sort()

I keep a common_json.py library and this is what I came up that I can call from:

common_json.print_sorted_keys(json_dict)

Note: If you are debugging inside the common_json.py library then you need to call this just by the method signature and omit the common_json. prefix.

Here is the full code:

def print_sorted_keys(json_dict):
    keys = json_dict.keys()
    list_of_keys = list(keys)
    list_of_keys.sort()
    print(list_of_keys)

Note: Coming from a Ruby background, it is interesting to note that the .sort() call is an inline sort i.e. it affects the list_of_keys object as opposed to returning a new object that is itself sorted. From a ruby perspective this would be a ! method since it modifies the current object.

Note: Ruby isn't fully consistent with ! methods so keep that in mind.


Posted In: #python


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK