I have a list of dictionaries. One of the fields of the dictionaries is "name"
. I am saving this list of dictionaries to file: with the command
json.dump(myList, fileName, indent=4)
but it is not being saved as I would like to. Since list
data-structure is a structure that allows sorting, how can I save it as json array ordered by a field of the dictionary elements?
For example consider this list of dictionaries:
l =[{'name':'c', 'val' : 9},{'name': 'a', 'val': 10}, {'name': 'b', 'val': 10}, {'name': 'aa', 'val': 10}]
I would like as output a jsons like:
[
{
"name": "a",
"val": 10
},
{
"name": "aa",
"val": 10
},
{
"name": "b",
"val": 10
},
{
"name": "c",
"val": 9
}
]