-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add json.tool option to avoid alphabetic sort of fields #65849
Comments
Currently when you use json.tool, fields are reordered alphabetically. |
This is the line in module I'm talking about: |
Here's a patch with a test case. |
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). |
It should be possible to also change the tool to use OrderDicts, though. |
Or does the data get decoded to a dict *before* it gets passed to the object_hook? Probably, in which case nevermind... |
The idea is to keep the same order as in input. |
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. |
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 :) |
Updated patch attached based on feedback from David. Thanks! |
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. |
May be make sorting keys not default? |
If you mean preserve order by default, then yes that would be a nice default. |
Thanks for the suggestions.
issue21650_v3.diff implements this idea. |
LGTM. |
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. |
New changeset 58a871227e5b by Berker Peksag in branch 'default': |
Thanks for the reviews. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: