I tried using json_normalize to flatten data but a part of the data is still not flattened. How can I get flattened data for this json.
Note: This is a sample json file but the data might increase in the nested part.
Code:
json_file = {
"data": "abc",
"data2": 123,
"results": {
"name": "w",
"more_data": [
{
"no": "111",
"code": 3
}
],
"id": 1
}
}
data = json_normalize(json_file)
data.to_csv('flatten.csv')
Result:
,data,data2,results.id,results.more_data,results.name
0,abc,123,1,"[{'no': '111', 'code': 3}]",w
results.more_data still gives me a json instead of flattening it.
How can I get it to work?
Expected output:
Since results.more_data is an array even this has to be flattened.
Eg:
,data,data2,results.id,results.more_data.0.no,results.more_data.0.code,results.name
0,abc,123,1, '111', "3",w
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…