简体   繁体   中英

How to sort JSON object by ID?

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

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM