# Sorting the Keys of a Python JSON Object

DevFeed: [Sorting the Keys of a Python JSON Object](<https://devfeed.tech/articles/sorting-the-keys-of-a-python-json-object-28235.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/python/2020/06/11/sorting-the-keys-of-a-python-json-object.html>)

Author: Fuzzygroup

Published: 2020-06-11T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Python](<https://devfeed.tech/topics/python.md>), [JSON](<https://devfeed.tech/topics/json.md>), [Sorting](<https://devfeed.tech/topics/sorting.md>), [Code](<https://devfeed.tech/topics/code.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [json](<https://devfeed.tech/tags/json.md>), [python](<https://devfeed.tech/tags/python.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [sorting](<https://devfeed.tech/tags/sorting.md>)

## AI overview

A Python technique for sorting the keys of a JSON object is presented using list conversion and the in-place .sort() method. The article also shows a reusable common_json.py helper function and notes how this differs from Ruby's conventions for mutating methods.

## Source excerpt

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.