所以我有这个嵌套的JSON,我正在努力转换成表格形式,因为它的形状,它可能会成为一个.xslx或. csv。
我试着这样做,但我没有工作。
# Load the JSON data into a pandas DataFrame
df = pd.read_json(response_data, typ='series')
# Convert the DataFrame to a flattened dictionary
flat_dict = pd.json_normalize(df.to_dict())
# Export the flattened dictionary to an Excel file
flat_dict.to_excel('output.xlsx', index=False)
JSON:
{
"A": [
{
"Price": 200,
"category": 620,
"service": {
"id": 15,
"name": "KAL",
"description": "Description",
"Validity": null,
"order": 0,
"services": [
{
"id": 100,
"financeable": true,
"benefit": {
"id": 235,
"name": "ZSX",
"Priced": null
},
"Execution": null,
"serviceId": 112,
"label": "Colab"
}
],
"selection": false
},
"creditTO": {
"id": 0,
"duration": 6,
"Type": "standard",
"Tax": 51,
"total": 400,
"promotion": false
}
}
],
"B": [
{
"Price": 200,
"category": 620,
"service": {
"id": 15,
"name": "BTX",
"description": "Description",
"Validity": null,
"order": 0,
"services": [
{
"id": 100,
"financeable": true,
"benefit": {
"id": 235,
"name": "ZSX",
"Priced": null
},
"Execution": null,
"serviceId": 112,
"label": "Colab"
}
],
"selection": false
},
"creditTO": {
"id": 0,
"duration": 9,
"Type": "standard",
"Tax": 51,
"total": 400,
"promotion": false
}
}
],
"C": [
{
"Price": 600,
"category": 620,
"service": {
"id": 15,
"name": "FLS",
"description": "Description",
"Validity": null,
"order": 0,
"services": [
{
"id": 100,
"financeable": true,
"benefit": {
"id": 235,
"name": "ZSX",
"Priced": null
},
"Execution": null,
"serviceId": 112,
"label": "Colab"
}
],
"selection": false
},
"creditTO": {
"id": 0,
"duration": 12,
"Type": "standard",
"Tax": 51,
"total": 400,
"promotion": false
}
}
],
"D": [
{
"Price": 705,
"category": 620,
"service": {
"id": 15,
"name": "TRW",
"description": "Description",
"Validity": null,
"order": 0,
"services": [
{
"id": 100,
"financeable": true,
"benefit": {
"id": 235,
"name": "ZSX",
"Priced": null
},
"Execution": null,
"serviceId": 112,
"label": "Colab"
}
],
"selection": false
},
"creditTO": {
"id": 0,
"duration": 18,
"Type": "standard",
"Tax": 67,
"total": 245,
"promotion": false
}
}
]
}
理想情况下,输出应该如下所示:
有什么建议可以用Python来转换它吗?可以是pandas或任何其他模块。
2条答案
按热度按时间jyztefdp1#
扩展到Corralien所写的内容,尝试这样做:
这是在假设
service.services
列下总是有一个列表的情况下工作的。输出:
bvpmtnay2#
您可以迭代第一级记录以创建单个 Dataframe ,然后将它们连接起来以获得预期的输出:
输出: