Home > OS >  Need to convert json to dataframe
Need to convert json to dataframe

Time:10-13

I have a below json , which I need to convert into dataframe

{'res':[{'columns':['col1','col2','col3'],'data':[{'row':['aaa','123','qqq'],'met':['none','none','none']},{'row':['bbb','456','rrr'],'met':['none','none','none']},
{'row':['ccc','789','sss'],'met':['none','none','none']}]}],'err':[]}

as

col1, col2, Col3
'aaa','123','qqq'
'bbb','456','rrr'
'ccc','789','sss'

CodePudding user response:

Try:

data = {
    "res": [
        {
            "columns": ["col1", "col2", "col3"],
            "data": [
                {"row": ["aaa", "123", "qqq"], "met": ["none", "none", "none"]},
                {"row": ["bbb", "456", "rrr"], "met": ["none", "none", "none"]},
                {"row": ["ccc", "789", "sss"], "met": ["none", "none", "none"]},
            ],
        }
    ],
    "err": [],
}


df = pd.DataFrame(
    [d["row"] for d in data["res"][0]["data"]],
    columns=data["res"][0]["columns"],
)
print(df)

Prints:

  col1 col2 col3
0  aaa  123  qqq
1  bbb  456  rrr
2  ccc  789  sss
  • Related