Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
CPallini 2-Jul-22 9:18am    
The code you've posted does not even try to print.
Laxmax1 2-Jul-22 18:43pm    
I know, this is a example of my last try but not works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900