How to Specify If Result Should Be Sorted in Python


In this example we will show how to specify if the result should be sorted or not using the sort_keys parameter in the json.dumps() method in Python.

Source Code

import json

my_dict = {
    'name': 'Jim',
    'age': 14,
    'id': '15'
}

python_to_json_1 = json.dumps(my_dict)
print(python_to_json_1)

python_to_json_2 = json.dumps(my_dict, indent=4, sort_keys=True)
print(python_to_json_2)

Output:

{"name": "Jim", "age": 14, "id": "15"}
{
    "age": 14,
    "id": "15",
    "name": "Jim"
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments