In the pydantic v1 there was an option to add kwargs which would get passed to json.dumps
via **dumps_kwargs
. However, in pydantic v2 if you try to add extra kwargs to BaseModel.json()
it fails with the error TypeError: `dumps_kwargs` keyword arguments are no longer supported.
Here is example code with a workaround using dict()
/model_dump()
. This is good enough as long as the types are simple, but it won’t work for the more complex data types that pydantic knows how to serialize.
Is there a way to get sort_keys
to work in pydantic v2 in general?
import json
from pydantic import BaseModel
class JsonTest(BaseModel):
b_field: int
a_field: str
obj = JsonTest(b_field=1, a_field="one")
# this worked in pydantic v1 but raises a TypeError in v2
# print(obj.json(sort_keys=True)
print(obj.model_dump_json())
# {"b_field":1,"a_field":"one"}
# workaround for simple objects
print(json.dumps(obj.model_dump(), sort_keys=True))
# {"a_field": "one", "b_field": 1}
2
Answers
Hi!
Try using the
model_dump
method and thesort_keys
parameter ofjson.dumps
to achieve sorting of keys in the JSON output inpydantic v2
.This will produce a
JSON
striing withsorted keys
. Themodel_dump
method returns a dictionary representation of thePydantic model
, and thenjson.dumps
is used with thesort_keys=True
parameter to sort the keys alphabeticaly in the resultingJSON string
.You can read more here: https://docs.pydantic.dev/latest/usage/serialization/
I’m not sure whether it is an elegant solution but you could leverage the fact that dictionaries (since python 3.7) preserve an order of elements: