-
-
Notifications
You must be signed in to change notification settings - Fork 16.5k
jsonify() ignores the sort order of an OrderedDict #974
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
Comments
JavaScript objects are not ordered. So it's pointless to take the order of an OrderedDict into account when converting it to JSON. If you need the order, jsonify its |
Agreed(on the "makes no sense in JS" part). However I've just noticed that the dict seems to be sorted by "key" after jsonifying, I've tried it with a pretty long list(more then 50 keys) so it doesn't looks like a coincidence. |
This is a feature. By default Flask will serialize JSON objects in a way that the keys are ordered. This is done in order to ensure that independent of the hash seed of the dictionary the return value will be consistent to not trash external HTTP caches. You can override the default behaviour by setting |
Actually, it seems there is a problem with |
This cannot be avoided. Object keys in JS have no order - just like python dict keys. If you need to keep the order you usually need to pass a list of AFAIK some engines keep the order for object literals, others don't - AFAIK the standard does not require any specific behavior related to the key order. |
No, I'm talking about the actual literal order in the JSON provided - nothing related to JS. That being arbitrary can definitely be avoided. |
ah.. I guess that depends on whether the json serializer honors the order or not |
I think it is worth revisiting this issue at some point since dicts will be ordered in Python 3.7 and actually somewhat are in 3.6 |
Hi, I would like to know whether this is a bug or a feature.
Example:
Result:
The text was updated successfully, but these errors were encountered: