Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to custom-sort a list of dict to use in json.dumps

Tags:

I have a list similar to

allsites = [     {         'A5': 'G',          'A10': 'G',          'site': 'example1.com',          'A1': 'G'     },      {         'A5': 'R',          'A10': 'Y',         'site': 'example2.com',          'A1': 'G'     } ] 

Which I use in a json.dumps:

data = { 'Author':"joe", 'data':allsites } print json.dumps(data,sort_keys=True,indent=4, separators=(',', ': ')) 

This outputs the following JSON:

{     "Author": "joe",     "data": [         {             "A1": "G",             "A10": "G",             "A5": "G",             "site": "example1.com"         },         {             "A1": "G",     (...) 

I would like the "data" section of this JSON string to be sorted via a custom key ("alphabet"), in the case above this would be site, A1, A5, A10 and actually look like:

{     "Author": "joe",     "data": [         {             "site": "example1.com",             "A1": "G",             "A5": "G",             "A10": "G"         },         {             "site": "example2.com",             "A1": "G",     (...) 

I read about custom sorting in the Sorting FAQ but it just gives a way to override the comparison function, not to mention that I do not know how to insert this into my code.

How to do that?

like image 661
WoJ Avatar asked Sep 18 '13 11:09

WoJ


People also ask

How do I sort a list of JSON objects?

Using json. dumps() function is one way to sort the JSON object. It is used to convert the array of JSON objects into a sorted JSON object. The value of the sort_keys argument of the dumps() function will require to set True to generate the sorted JSON objects from the array of JSON objects.

Can you have a list of dictionaries in JSON?

In the same way that JSON objects can be nested within arrays or arrays nested within objects, Python dictionaries can be nested within lists or lists nested within dictionaries. So pretty much any JSON data structure can be translated into a complex Python data object.

Can you JSON dumps a list?

To convert a Python List to JSON, use json. dumps() function. dumps() function takes list as argument and returns a JSON String.


1 Answers

Since python dicts are unordered collections, use collections.OrderedDict with a custom sort:

from collections import OrderedDict import json  allsites = [     {         'A5': 'G',         'A10': 'G',         'site': 'example1.com',         'A1': 'G'     },     {         'A5': 'R',         'A10': 'Y',         'site': 'example2.com',         'A1': 'G'     } ]  sort_order = ['site', 'A1', 'A5', 'A10'] allsites_ordered = [OrderedDict(sorted(item.iteritems(), key=lambda (k, v): sort_order.index(k)))                     for item in allsites]  data = {'Author': "joe", 'data': allsites_ordered} print json.dumps(data, indent=4, separators=(',', ': ')) 

prints:

{     "data": [         {             "site": "example1.com",             "A1": "G",             "A5": "G",             "A10": "G"         },         {             "site": "example2.com",             "A1": "G",             "A5": "R",             "A10": "Y"         }     ],     "Author": "joe" } 
like image 102
alecxe Avatar answered Oct 26 '22 19:10

alecxe