I want to sort a JSON object of arrays on the basis of the key "score". Here's a sample data:
[
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
},
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
}
]
}
]
Sample output:
[
{
"name": "Hilary Carr",
"submissions": [
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
]
},
{
"name": "Frederick Williamson",
"submissions": [
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
},
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
}
]
}
]
I tried the solution given in this question Sort a JSON using Python, but my JSON data is mutilated by this. Here's what I tried:
for i in range(len(inputData)):
sample_data_sort = dict(inputData)
sample_data_sort = sorted(inputData[i]['submissions'], key=lambda x : x['score'], reverse=True)
print(sample_data_sort)
Output that I get:
[
{
"name": "Laudantium deleniti beatae fuga.",
"date": "05/12/2021",
"score": 37
}
],
[
{
"name": "Animi facere excepturi.",
"date": "01/02/2021",
"score": 100
},
{
"name": "Expedita architecto voluptas autem veniam.",
"date": "03/05/2009",
"score": 47
}
]
As you can see, I get the mutilated JSON back. Can you please give some tips to rectify this problem, so that the output that I get is similar to the input?
inputData[i]['submissions'].sort(key=lambda x : x['score'], reverse=True))