Skip to content
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

Open
3 of 13 tasks
AqemiArnold opened this issue Sep 13, 2023 · 15 comments · May be fixed by pydantic/pydantic-core#1637
Open
3 of 13 tasks

Add sorting keys to model_dump_json #7424

AqemiArnold opened this issue Sep 13, 2023 · 15 comments · May be fixed by pydantic/pydantic-core#1637
Assignees

Comments

@AqemiArnold
Copy link

AqemiArnold commented Sep 13, 2023

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

I may be missing something, I do apologize in advance.

From reading the docs, it appears that the .model_dump_json() method of the BaseModel 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

@dmontagu
Copy link
Contributor

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 model_dump_json in pydantic-core. @davidhewitt is it hard to get serde to dump JSON with keys in sorted order? It feels like something there might be an existing option for, or maybe not.

@AqemiArnold
Copy link
Author

AqemiArnold commented Sep 14, 2023

I'd be more than happy to contribute to this awesome project however I do not have any experience in Rust, yet 👀 .

@antazoey
Copy link

antazoey commented Oct 12, 2023

In fact, there are other kwargs to JSON dumping that un-available such as separators.
Probably all captured in #6606 ?

@AqemiArnold
Copy link
Author

Hi guys,

Would you mind giving us an update on this please?
I tried to look around with the aforementioned issues and open PR and could not make sense of it 😢

Thank you for your time :)

@antazoey
Copy link

antazoey commented Dec 12, 2023

Why is this closed? @AqemiArnold will be be able to use sort_keys in model_dump_json() the same way we were able to use sort_keys in json() from v1? That is the issue here - a whole kwarg is unavailable.

Note: our schema requires sorted order in JSON string but it doesn't really matter in Python dicts.

@AqemiArnold AqemiArnold reopened this Dec 16, 2023
@AqemiArnold
Copy link
Author

I'm sorry I did not understand the message.

@AqemiArnold
Copy link
Author

What are you (@antazoey) suggesting, and what should I do?

@antazoey
Copy link

What are you (@antazoey) suggesting, and what should I do?

Here is the docs in the standard JSON library for dump: https://docs.python.org/3/library/json.html#basic-usage
Notice it has a kwarg sort_keys. It's doc is:

If sort_keys is true (default: False), then the output of dictionaries will be sorted by key.

This kwarg also works in Pydantic V1 in a model's .json() method.
This kwarg DOES NOT working on Pydantic v2 in a model's model_dump_json() method.

My feature request here is to add support for the sort_keys kwarg to Pydantic v2 the same way the standard JSON has and the same way Pydantic v1 has.

I have never contributed to Pydantic directly so I am not super aware of the technical challenges necessities in order to achieve this feature.

@hunsche
Copy link

hunsche commented Mar 10, 2024

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!

@AqemiArnold
Copy link
Author

It works but it is very slow for most of the use cases I have...

@slobodaapl
Copy link

slobodaapl commented Aug 15, 2024

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!

Great snippet but this sadly doesn't work if you're using some of the custom PyDantic validators, like DirectoryPath for inst.

@edgarrmondragon
Copy link

👍 to this and adding that it'd be nice to control this from the ConfigDict, e.g.

class MyModel(pydantic.BaseModel):
    model_config = pydantic.ConfigDict(sort_keys="asc")

@TweedBeetle
Copy link

This workaround is working for me:

https://gist.github.com/TweedBeetle/f56125de214318dc644fe5868bf5cd84

@chrisjsewell
Copy link

Just to note some technical details on this:
pydantic-core uses the preserve_order feature of serde_json:

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

@aezomz
Copy link

aezomz commented Feb 10, 2025

Is this still open?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.