'Load from .json file, sort data according to value then save it to same .json file

The JSON file I have:

[{
"key_a": "value",
"key_b": [{
        "key_a": "c",
        "key_b": null
    },
    {
        "key_a": "b",
        "key_b": null
    }]
}]

The format I want:

[{
    "key_a": "value",
    "key_b": [{
            "key_a": "b",
            "key_b": null
        },
        {
            "key_a": "c",
            "key_b": null
        }]
}]

How can I sort child elements according to the value of key (for example according to 'key_a') then save it to the source JSON file from where data was loaded?

What I have in mind is something like this:

def read_json_file():
    with open(filename) as file:
        var = json.load(file)
    file.close()

def sort_json_var():
    ...

def write_json_file():
    with open (filename, 'w') as file:
        json.dump(data, file, indent=4)
    file.close()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source