How could I print all the keys with their values of this nested json, it has become very difficult to print the data correctly, does anyone have an idea?
Sometimes financeInfo does not come in the response but its data does and sometimes financeInfo has another level called financeInfoAttributes
when I print I get this response structure.
codeId Financeinfo
fc-5999 [{"date": "2022-01-30","totalReturn": 0.022425456852}...]
Data Sample:
{
"codeId": "fc-5599",
"financeInfo": [
{
"date": "2022-01-30",
"totalReturn": 0.022425456852
},
{
"date": "2022-01-30",
"totalReturn": 0.022425456852
},
{
"date": "2022-01-30",
"totalReturn": 0.022425456852
},
{
"date": "2022-01-30",
"totalReturn": 0.022425456852
},
{
"date": "2022-01-30",
"totalReturn": 0.022425456852
},
{
"date": "2022-02-28",
"totalReturn": -0.0424735070051586,
"financeInfoAttributes": [
{
"attributeId": "a-256",
"value": "12.032791372796499"
},
{
"attributeId": "a-257",
"value": "9.975964795996589"
},
{
"attributeId": "a-258",
"value": "4.719852927810759"
},
{
"attributeId": "a-259",
"value": "4.18144793134823"
},
]
}
What I have tried:
new_items = []
for item in items["financeInfo"]:
if "financeInfoAttributes" not in item:
new_items.append(
{"date": item.get("date"), "totalReturn": item.get("totalReturn")}
)
else:
for item2 in item["financeInfoAttributes"]:
new_items.append(
{"date": item.get("date"), "totalReturn": item.get("totalReturn"),
"attributeId": item2.get("attributeId"), "value": item2.get("value")}
)
But not works.