Looking for a way to simply the sort json object function in python

Viewed 36

this function takes two argument, first one is the json object, second one is the sorted dictionary. then it check if it is a dict or list. If list, it sort the values based on given key from sorted_by dictonary. Question: As for e loop is same, I can put it into a different function but how do i run line obj[e] = sort_json_object_list(obj[e], sorted_by) from another function

def sort_json_object_list(obj, sorted_by):
    if isinstance(obj, dict):
        for e in obj:
            if isinstance(obj[e], list):
                if e in sorted_by:
                    print(f'sorting {e} by {sorted_by[e]}')
                    obj[e].sort(key=lambda x: x[sorted_by[e]])
                    obj[e] = sort_json_object_list(obj[e], sorted_by)
    if isinstance(obj, list):
        for obj_list in obj:
            for e in obj_list:
                if isinstance(obj_list[e], list):
                    if e in sorted_by:
                        print(f'sorting {e} by {sorted_by[e]}')
                        obj_list[e].sort(key=lambda x: x[sorted_by[e]])
                        obj_list[e] = sort_json_object_list(
                            obj_list[e], sorted_by)
    return obj

Random Json Input which Need to be sorted

{
      "types": [
            "sales",
            "accounting"
      ],
      "accounting": [
            {
                  "firstName": "John",
                  "lastName": "Doe",
                  "age": 23,
                  "departments": [
                        {
                              "id": 32,
                              "title": "head"
                        },
                        {
                              "id": 3,
                              "title": "credit"
                        }
                  ]
            },
            {
                  "firstName": "Mary",
                  "lastName": "Smith",
                  "age": 32,
                  "departments": [
                        {
                              "id": 123,
                              "title": "Gretta"
                        },
                        {
                              "id": 3,
                              "title": "Patti"
                        }
                  ]
            }
      ],
      "sales": [
            {
                  "firstName": "Sally",
                  "lastName": "Green",
                  "age": 27,
                  "departments": [
                        {
                              "id": 64,
                              "title": "Gretta"
                        },
                        {
                              "id": 34,
                              "title": "Patti"
                        }
                  ]
            },
            {
                  "firstName": "Jim",
                  "lastName": "Galley",
                  "age": 41,
                  "departments": [
                        {
                              "id": 23,
                              "title": "Gretta"
                        },
                        {
                              "id": 86,
                              "title": "Patti"
                        }
                  ]
            }
      ]
}

Given Json input will be sorted based on Sorted_by dict

{
    "accounting": "age",
    "departments": "id",
    "sales": "age"
}

expected_output:

{'types': ['accounting', 'sales'],
 'accounting': [{'firstName': 'John',
   'lastName': 'Doe',
   'age': 23,
   'departments': [{'id': 3, 'title': 'credit'}, {'id': 32, 'title': 'head'}]},
  {'firstName': 'Mary',
   'lastName': 'Smith',
   'age': 32,
   'departments': [{'id': 3, 'title': 'Patti'},
    {'id': 123, 'title': 'Gretta'}]}],
 'sales': [{'firstName': 'Sally',
   'lastName': 'Green',
   'age': 27,
   'departments': [{'id': 34, 'title': 'Patti'},
    {'id': 64, 'title': 'Gretta'}]},
  {'firstName': 'Jim',
   'lastName': 'Galley',
   'age': 41,
   'departments': [{'id': 23, 'title': 'Gretta'},
    {'id': 86, 'title': 'Patti'}]}]}
0 Answers
Related