-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 sorting keys to model_dump_json #7424
Add sorting keys to model_dump_json #7424
Comments
I think this is a good feature request, as it is nice to get JSON dumped in a way that doesn't depend on the order of fields etc. (Not to mention sometimes it's just nice to have it sorted.) I personally would welcome a PR adding this though I would defer to @samuelcolvin if he felt it was better not to. Either way, adding the feature would need to happen in the implementation of |
I'd be more than happy to contribute to this awesome project however I do not have any experience in Rust, yet 👀 . |
In fact, there are other kwargs to JSON dumping that un-available such as |
Hi guys, Would you mind giving us an update on this please? Thank you for your time :) |
Why is this closed? @AqemiArnold will be be able to use Note: our schema requires sorted order in JSON string but it doesn't really matter in Python dicts. |
I'm sorry I did not understand the message. |
What are you (@antazoey) suggesting, and what should I do? |
Here is the docs in the standard JSON library for
This kwarg also works in Pydantic V1 in a model's My feature request here is to add support for the I have never contributed to Pydantic directly so I am not super aware of the technical challenges necessities in order to achieve this feature. |
Hey everyone, sharing this snippet that's been a lifesaver for me 🚀: instance_dict = instance.model_dump()
json_str = json.dumps(instance_dict, sort_keys=True) Hope it helps anyone in the same boat! |
It works but it is very slow for most of the use cases I have... |
Great snippet but this sadly doesn't work if you're using some of the custom PyDantic validators, like DirectoryPath for inst. |
👍 to this and adding that it'd be nice to control this from the class MyModel(pydantic.BaseModel):
model_config = pydantic.ConfigDict(sort_keys="asc") |
This workaround is working for me: https://gist.github.com/TweedBeetle/f56125de214318dc644fe5868bf5cd84 |
Just to note some technical details on this:
to "break" that ordering, I guess one would need to call https://docs.rs/serde_json/latest/serde_json/value/enum.Value.html#method.sort_all_objects or https://docs.rs/serde_json/latest/serde_json/map/struct.Map.html#method.sort_keys somewhere in the serialisation code |
Is this still open? |
Initial Checks
Description
I may be missing something, I do apologize in advance.
From reading the docs, it appears that the
.model_dump_json()
method of theBaseModel
does not support the sorting of the keys when serializing objects to json.Would it be possible to add a boolean of some sort to enable recursive sorting of the keys, as follows:
.model_dump_json(sort_keys=True)
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: