I want to sort the JSON below by ID.
This is unsuccessful:
records2 = sorted(records, key=lambda d: d["id"])
Question
How do I sort this JSON code by ID?
{
"item": [
{
"id": "2",
"name": "name1",
"arr": [
"a",
"b"
]
},
{
"id": "1",
"name": "name2",
"arr": [
"c",
"d"
]
}
]
}
答案 0 :(得分:0)
try this
records2 ={"items" : sorted(records["item"], key=lambda d: d["id"])}
sorted work on array(list) and u must go to items whitch is array(list )