Sort JSON output in Python

I have a problem with JSON in python.

In fact, if I try to execute this code, python gives me a sorted JSON string!

For example:

values = {'profile' : 'testprofile', 'format': 'RSA_RC4_Sealed', 'enc_key' : base64.b64encode(chiave_da_inviare), 'request' : base64.b64encode(data) } values_json = json.dumps(values, sort_keys=False, separators=(',', ':')) 

and this is the result:

 {"profile":"testprofile","enc_key":"GBWo[...]NV6w==","request":"TFl[...]uYw==","format":"RSA_RC4_Sealed"} 

As you can see, I tried using "sort_keys = False", but nothing changed.

How can I stop Python by sorting JSON strings?

+53
json python sorting
May 05 '10 at 3:01
source share
6 answers

You save your values ​​in a python dict that has no concept of ordering at all, it's just a key => value map. Thus, your products lose their order when you put them in the "values" variable.

In fact, the only way to get a deterministic order is to use "sort_keys = True", which I assume puts them in an alphanumeric order. Why is this order so important?

+50
May 05 '10 at 15:06
source share

Try OrderedDict from the standard collections library:

 >>> import json >>> from collections import OrderedDict >>> values = OrderedDict([('profile','testprofile'), ('format', 'RSA_RC4_Sealed'), ('enc_key', '...'), ('request', '...')]) >>> json.dumps(values, sort_keys=False) '{"profile": "testprofile", "format": "RSA_RC4_Sealed", "enc_key": "...", "request": "..."}' 

Unfortunately, this function is New in version 2.7 for collections

+90
May 23 '12 at 12:44
source share

OrderedDict, as discussed elsewhere, is the majority of the solutions to your problem, and "ObjDict" might be even better.

However, if you need an order saved during loading, you will also need json.loads () to load the values ​​into the OrderedDict. To do this, use

 from collections import OrderedDict values=json.loads(jsontext,object_pairs_hook=OrderedDict) 

Otherwise, even if the json file is in order, this order will be lost upon download.

Perhaps the best solution would be to use "ObjDict" instead of OrderedDict. This requires a pip installation object. ObjDict still maintains order, as in OrderedDict, but also supports JSON and improves the handling of this example.

 from objdict import ObjDict values = ObjDict("""{"profile" : "testprofile", "format": "RSA_RC4_Sealed" }""") values.enc_key = base64.b64encode(chiave_da_inviare) values.request = base64.b64encode(data) values_json = values.dumps(separators=(',', ':')) 
+14
Feb 06 '16 at 5:02
source share

If you specify sort_keys=False , then Python will simply print the elements in the order in which they appear in the base dict. In some cases, this may coincide with the default alphanumeric sort order. In your example, the ARE keys are not even sorted, since the "format" appears after the "request". Regardless of the fact that the sort_keys parameter is still valid, as this code example demonstrates:

 >>> import json >>> json.dumps({"a":5, "b":6, "c":7}, sort_keys=False) '{"a": 5, "c": 7, "b": 6}' 
+6
May 05 '10 at 15:08
source share

The keys are not sorted: "profile", "enc_key", "request", "format".

It looks like you want them to appear in the same order you created them in the dictionary, but the dictionaries are incorrect, they don’t remember the order you entered.

There are several SortedDict options that you can use, but the json encoder will not know to use it to get the keys in the order you want.

+4
May 05 '10 at
source share

You can sort JSON data using simple json.dumps as

 sotred_json = json.dumps(values, sort_keys=True) 

If you do not want to sort, just provide

 unsotred_json = json.dumps(values) 

or:

 unsotred_json = json.dumps(values, sort_keys=False) 
0
Jul 19 '18 at 10:47
source share



All Articles