Код: Выделить всё
{
"abc": {
"key1": "val1"
},
"xyz": {
"key2": "val2"
},
"pqr": {
"key3": "val3"
},
"123": {
"key4": "val4"
}
}
Это код, который я использую:
Код: Выделить всё
import json
sorted_json_data = None
filename = 'data.json'
try:
with open(filename, "r") as f:
json_data = json.load(f)
sorted_json_data = json.dumps(json_data, sort_keys=True, indent=4)
except ValueError as err:
print(f"{filename}: Failed to decode: {err}")
if sorted_json_data is not None:
print(f"{filename}: File is not sorted")
else:
print(f"{filename}: File is sorted")
Подробнее здесь: https://stackoverflow.com/questions/786 ... -is-sorted