'how to handle complex json where key is not always present using python?
I need support in getting the value of Key issues which is not always present in JSON.
my JSON object is as below -
{
"records":[
{
"previousAttempts": [],
"id": "aaaaa-aaaaaa-aaaa-aaa",
"parentId": null
},
{
"previousAttempts": [],
"id": "aaaaa-aaaaaa-aaaa-aaa",
"parentId": null,
"issues":[
{
"type": "warning",
"category": "General"
},
{
"type": "warning",
"category": "General"
}
]
}
]
}
Solution 1:[1]
You can use Pydantic to deserialize JSON string to Pydantic object. You can make a dict object from pydantic object then. You can set default value for fields you need.
You can use validators to validate loaded values.
from pydantic import BaseModel, Field
class AttemptModel(BaseModel):
pass
class IssueModel(BaseModel):
type: str
category: str
class RecordModel(BaseModel):
id: str
parent_id: int = Field(alias='parentId', default=None)
previous_attempts: list[AttemptModel] = Field(alias='previousAttempts')
issues: list[IssueModel] = Field(default=[])
class DataModel(BaseModel):
records: list[RecordModel]
data = """{
"records":[
{
"previousAttempts": [],
"id": "aaaaa-aaaaaa-aaaa-aaa",
"parentId": null
},
{
"previousAttempts": [],
"id": "aaaaa-aaaaaa-aaaa-aaa",
"parentId": null,
"issues":[
{
"type": "warning",
"category": "General"
},
{
"type": "warning",
"category": "General"
}
]
}
]
}
"""
data_model = DataModel.parse_raw(data)
print(data_model.dict())
You can find addition documentation here: https://pydantic-docs.helpmanual.io/ Also it contain installation steps.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |