How to sort JSON object by ID?

时间:2017-12-17 08:19:27

标签: python json

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"
      ]
    }
  ]
}

1 个答案:

答案 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 )