I have a pydantic model as follows.
from pydantic import Json, BaseModel
class Foo(BaseModel):
id: int
bar: Json
Foo.bar can parse JSON string as input and store it as a dict which is nice.
foo = Foo(id=1, bar='{"foo": 2, "bar": 3}')
type(foo.bar) #outputs dict
And if i want the entire object to be a dict I can do
foo.dict()
#outputs
{'id': 1, 'bar': {'foo': 2, 'bar': 3}}
But how can I export bar as JSON string as following
{'id': 1, 'bar': '{"foo": 2, "bar": 3}'}
I want to write JSON back into the database.