You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
add json.tool option to avoid alphabetic sort of fields #65849
Currently when you use json.tool, fields are reordered alphabetically.
In source code the value of sort_keys is hardcoded to be true.
It should be easy to expose this option as command line parameter.
I don't really understand the point of this. The "unsorted" output order will be unpredictable for the user (it isn't necessarily the same as the order of fields in the input data).
Yes but the input is turned into a dict, and dicts do not preserve order. Further, what is passed to the object_hook is already a dict, so the order is already lost before object_hook is called.
Since the parser (or at least the Python version of the parser) first turns the input into a list of pairs, and a Python OrderedDict can be instantiated from a list of pairs, it would theoretically be possible to extend the object_hook API to allow an OrderedDict to be used on decode. Exactly how to do that so as to retain backward compatibility is a bit of a question.
So, it is *possible* to achieve your aim, but it isn't as simple as allowing sort= to be set False.
IMO being able to preserve the order of the input when desired (ie: use an OrderedDict object_hook) would be a nice feature to have.
You can define object_pairs_hook, and use that to return an OrderedDict. So it should be possible to do this without changing the json module itself. This is actually documented as a place to use OrderedDict. Guess I should have read the docs instead of the code :)
To me, "--unsorted" implies arbitrary ordering such as the order generated by regular dictionaries. I think the option should be "--order-preserving" or some such. The option name needs to make it clear that the output is in the same order an the input.
As side from the option name, I'm +1 on the patch. It is very annoying to look at scrambled JSON when the original presentation order made more logical sense.
The patch looks good. One nit, the phrase "sorted by their key" has an odd ring to it and is mildly confusing, though technically correct. Perhaps, "sorted alphabetically by key" would be better for most folks.
Activity
PavelKazlou commentedon Jun 3, 2014
Currently when you use json.tool, fields are reordered alphabetically.
In source code the value of sort_keys is hardcoded to be true.
It should be easy to expose this option as command line parameter.
PavelKazlou commentedon Jun 3, 2014
This is the line in module I'm talking about:
json.dump(obj, outfile, sort_keys=True, indent=4)
berkerpeksag commentedon Jun 3, 2014
Here's a patch with a test case.
pitrou commentedon Jun 5, 2014
I don't really understand the point of this. The "unsorted" output order will be unpredictable for the user (it isn't necessarily the same as the order of fields in the input data).
bitdancer commentedon Jun 5, 2014
It should be possible to also change the tool to use OrderDicts, though.
bitdancer commentedon Jun 5, 2014
Or does the data get decoded to a dict *before* it gets passed to the object_hook? Probably, in which case nevermind...
PavelKazlou commentedon Jun 6, 2014
The idea is to keep the same order as in input.
bitdancer commentedon Jun 6, 2014
Yes but the input is turned into a dict, and dicts do not preserve order. Further, what is passed to the object_hook is already a dict, so the order is already lost before object_hook is called.
Since the parser (or at least the Python version of the parser) first turns the input into a list of pairs, and a Python OrderedDict can be instantiated from a list of pairs, it would theoretically be possible to extend the object_hook API to allow an OrderedDict to be used on decode. Exactly how to do that so as to retain backward compatibility is a bit of a question.
So, it is *possible* to achieve your aim, but it isn't as simple as allowing sort= to be set False.
IMO being able to preserve the order of the input when desired (ie: use an OrderedDict object_hook) would be a nice feature to have.
bitdancer commentedon Jun 6, 2014
Wait, I read the code wrong.
You can define object_pairs_hook, and use that to return an OrderedDict. So it should be possible to do this without changing the json module itself. This is actually documented as a place to use OrderedDict. Guess I should have read the docs instead of the code :)
berkerpeksag commentedon Jun 14, 2014
Updated patch attached based on feedback from David. Thanks!
rhettinger commentedon Nov 1, 2014
To me, "--unsorted" implies arbitrary ordering such as the order generated by regular dictionaries. I think the option should be "--order-preserving" or some such. The option name needs to make it clear that the output is in the same order an the input.
As side from the option name, I'm +1 on the patch. It is very annoying to look at scrambled JSON when the original presentation order made more logical sense.
serhiy-storchaka commentedon Nov 1, 2014
May be make sorting keys not default?
rhettinger commentedon Nov 1, 2014
If you mean preserve order by default, then yes that would be a nice default.
berkerpeksag commentedon Nov 2, 2014
Thanks for the suggestions.
issue21650_v3.diff implements this idea.
serhiy-storchaka commentedon Nov 3, 2014
LGTM.
rhettinger commentedon Nov 5, 2014
The patch looks good. One nit, the phrase "sorted by their key" has an odd ring to it and is mildly confusing, though technically correct. Perhaps, "sorted alphabetically by key" would be better for most folks.
python-dev commentedon Nov 10, 2014
New changeset 58a871227e5b by Berker Peksag in branch 'default':
Issue bpo-21650: Add an
--sort-keysoption to json.tool CLI.https://hg.python.org/cpython/rev/58a871227e5b
berkerpeksag commentedon Nov 10, 2014
Thanks for the reviews.