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?
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.
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.
To convert a Python List to JSON, use json. dumps() function. dumps() function takes list as argument and returns a JSON String.
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" }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With