我有一个json,其中包含python字段名不能包含的字符:
{
"mlflow.source.git.commit": "fbe812fe",
"other.key": "other.value"
}
如何使用pydantic
来解析这样的json?我想给它一个别名和键名,比如
class Tags(pydantic.BaseModel):
commit = field(key="mlflow.source.git.commit", type=str)
这可以使用pydantic.Field(alias="...")
:
import pydantic
class Tags(pydantic.BaseModel):
commit : str = pydantic.Field(alias="mlflow.source.git.commit")
data = {
"mlflow.source.git.commit": "fbe812fe",
"other.key": "other.value"
}
t = Tags(**data)