0

I have a following Json as an output and further need to organize the json in descending order of the sal value

[
     {
        "sal": 12,
        "Stack ": "Java",
        "EmpID": "00030010-0010-0000-0000-000000000121"
    },
    {
        "sal": 24,
        "Stack ": ".net",
        "EmpID": "00030010-0010-0000-0000-000000000144"
    },
    {
        "sal": 14,
        "Stack ": "Ruby",
        "EmpID": "00030010-0010-0000-0000-000000000198"
    }
]

please help me achieving the same

devPy
  • 53
  • 4
  • you can use , `your_list = sorted(your_list, key=lambda x: x.get('sal'), reverse=True) `, https://docs.python.org/3/howto/sorting.html – Shijith Dec 20 '21 at 14:09

2 Answers2

5

You can use the sorted built-in function using a specific key for sorting, like this:

sorted(list_of_dicts, key=lambda d: d['sal'], reverse=True)

Edit:

However, a question like this one was already answered here with some more options: https://stackoverflow.com/a/73050/6279885

Sherlock Bourne
  • 490
  • 1
  • 5
  • 10
1

You can sort the JSON data based on the value of the sal key

data.sort(key=lambda x: x["sal"])
Thekingis007
  • 627
  • 2
  • 16