I’ve 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 output:
{
"profile": "testprofile",
"enc_key": "GBWo[...]NV6w==",
"request": "TFl[...]uYw==",
"format": "RSA_RC4_Sealed"
}
As you can see, I tried to use “sort_keys=False” but nothing changed.
How can I stop Python sorting my JSON strings?
You are storing your values into a Python
dictwhich has no inherent notion of ordering at all, it’s just a key-to-value map. So your items lose all ordering when you place them into thevaluesvariable.In fact the only way to get a deterministic ordering would be to use
sort_keys=True, which I assume places them in alphanumeric ordering. Why is the order so important?