I'm trying to sort JSON by high score, but this doesn't work. My JSON:
{"players": [{"test": [{"high_score": 1000}]}, {"test1": [{"high_score": 1200}]}, {"test2": [{"high_score": 3000}]}]}
My Python:
with open('score.json', "r") as json_file:
data = json.load(json_file)
json_file.close()
sorted_obj = data
sorted_obj['players'] = sorted(data['players'], key=lambda x: x['high_score'], reverse=True)
print(sorted_obj)
Output:
sorted_obj['players'] = sorted(data['players'], key=lambda x: x['high_score'], reverse=True)
KeyError: 'high_score''
I want output to be:
{"players": [{"test2": [{"high_score": 3000}]}, {"test1": [{"high_score": 1200}]}, {"test": [{"high_score": 1000}]}]}
Does anyone know how to solve this? Thanks