-1

I'm sorry if that question has already been asked before, I read some topics but it didn't help me so far.

I get this JSON response from a software API :

 {
    u 'code': 0, u 'message': u 'Found: 367', u 'objects': {
        u 'ApplicationSolution::15056': {
            u 'fields': {
                u 'id': u '15056',
                u 'name': u 'R'
            },
            u 'message': u '',
            u 'code': 0,
            u 'class': u 'ApplicationSolution',
            u 'key': u '15056'
        },
        u 'ApplicationSolution::15758': {
            u 'fields': {
                u 'id': u '15758',
                u 'name': u 'Z'
            },
            u 'message': u '',
            u 'code': 0,
            u 'class': u 'ApplicationSolution',
            u 'key': u '15758'
        },
        u 'ApplicationSolution::15053': {
            u 'fields': {
                u 'id': u '15053',
                u 'name': u 'E'
            },
            u 'message': u '',
            u 'code': 0,
            u 'class': u 'ApplicationSolution',
            u 'key': u '15053'
        },
        u 'ApplicationSolution::15050': {
            u 'fields': {
                u 'id': u '15050',
                u 'name': u 'C'
            },
            u 'message': u '',
            u 'code': 0,
            u 'class': u 'ApplicationSolution',
            u 'key': u '15050'
        },
}

I would like to sort this response with ApplicationSolution name (objects > ApplicationSolution:xxxxx > fields > name). I try to do this with sorted but I didn't find the right syntax :(

Anyone could help me please ?

Thank you so much !

Kementari
  • 23
  • 1
  • 5
  • 3
    What have you tried ? How did it end ? – gogaz Aug 24 '18 at 08:45
  • 4
    You cannot sort a dictionary, so you'll have to let us know how you expect the output to look like. – thebjorn Aug 24 '18 at 08:47
  • Could you please provide [MCVE](https://stackoverflow.com/help/mcve)? – sophros Aug 24 '18 at 08:47
  • 1
    Possible duplicate of [In Python, how do I iterate over a dictionary in sorted order?](https://stackoverflow.com/questions/364519/in-python-how-do-i-iterate-over-a-dictionary-in-sorted-order) – meowgoesthedog Aug 24 '18 at 08:48
  • @thebjorn I need the same output, but sorted with ApplicationSolution name. I guess I have to convert this dictionary to a list, it's not a problem, but I'm stucked with the usage of sorted then. – Kementari Aug 24 '18 at 08:58

2 Answers2

2

I would first convert the dictionary to a list of 2-tuples, and then sort the list with the appropriate field. It can be as simple as (assuming the global dict is called j):

sorted([(k,v) for k,v in j['objects'].items()], key=(lambda x: x[1]['fields']['name']))
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • This is exactly what I needed, I tried to do something like that but my syntax wasn't good. Thanks for your answer ! – Kementari Aug 24 '18 at 09:51
0

If d is your dict, then

def fieldname((k, v)):
    return v['fields']['name']
d['objects'] = list(sorted(d['objects'].items(), key=fieldname))

would do it in-situ.

thebjorn
  • 26,297
  • 11
  • 96
  • 138