首页 > 解决方案 > 使用 Python 将嵌套的 JSON 转换为 CSV

问题描述

我正在使用 python,我需要将嵌套的 JSON 转换为 CSV。我正在从 Xero 的 API 导出数据。我不确定如何处理嵌套以将其转换为 CSV 格式的可读输出。这是我正在处理的 JSON:

{
"Reports": [
    {
        "Fields": [],
        "ReportDate": "18 August 2020",
        "ReportID": "TrialBalance",
        "ReportName": "Trial Balance",
        "ReportTitles": [
            "Trial Balance",
            "Demo Company (NZ)",
            "As at 31 March 2020"
        ],
        "ReportType": "TrialBalance",
        "Rows": [
            {
                "Cells": [
                    {
                        "Value": "Account"
                    },
                    {
                        "Value": "Debit"
                    },
                    {
                        "Value": "Credit"
                    },
                    {
                        "Value": "YTD Debit"
                    },
                    {
                        "Value": "YTD Credit"
                    }
                ],
                "RowType": "Header"
            },
            {
                "RowType": "Section",
                "Rows": [
                    {
                        "Cells": [
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "7d05a53d-613d-4eb2-a2fc-dcb6adb80b80"
                                    }
                                ],
                                "Value": "Sales (200)"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "7d05a53d-613d-4eb2-a2fc-dcb6adb80b80"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "7d05a53d-613d-4eb2-a2fc-dcb6adb80b80"
                                    }
                                ],
                                "Value": "1043.48"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "7d05a53d-613d-4eb2-a2fc-dcb6adb80b80"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "7d05a53d-613d-4eb2-a2fc-dcb6adb80b80"
                                    }
                                ],
                                "Value": "5826.07"
                            }
                        ],
                        "RowType": "Row"
                    }
                ],
                "Title": "Revenue"
            },
            {
                "RowType": "Section",
                "Rows": [
                    {
                        "Cells": [
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "2e277847-022c-48f3-8467-0207230004d6"
                                    }
                                ],
                                "Value": "Light, Power, Heating (445)"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "2e277847-022c-48f3-8467-0207230004d6"
                                    }
                                ],
                                "Value": "184.57"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "2e277847-022c-48f3-8467-0207230004d6"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "2e277847-022c-48f3-8467-0207230004d6"
                                    }
                                ],
                                "Value": "837.92"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "2e277847-022c-48f3-8467-0207230004d6"
                                    }
                                ],
                                "Value": ""
                            }
                        ],
                        "RowType": "Row"
                    }
                ],
                "Title": "Expenses"
            },
            {
                "RowType": "Section",
                "Rows": [
                    {
                        "Cells": [
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "ac993f75-035b-433c-82e0-7b7a2d40802c"
                                    }
                                ],
                                "Value": "Business Bank Account (090)"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "ac993f75-035b-433c-82e0-7b7a2d40802c"
                                    }
                                ],
                                "Value": "1094.25"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "ac993f75-035b-433c-82e0-7b7a2d40802c"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "ac993f75-035b-433c-82e0-7b7a2d40802c"
                                    }
                                ],
                                "Value": "9973.88"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "ac993f75-035b-433c-82e0-7b7a2d40802c"
                                    }
                                ],
                                "Value": ""
                            }
                        ],
                        "RowType": "Row"
                    }
                ],
                "Title": "Assets"
            },
            {
                "RowType": "Section",
                "Rows": [
                    {
                        "Cells": [
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "568f2e9a-0870-46cc-8678-f83f132ed4e3"
                                    }
                                ],
                                "Value": "Accounts Payable (800)"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "568f2e9a-0870-46cc-8678-f83f132ed4e3"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "568f2e9a-0870-46cc-8678-f83f132ed4e3"
                                    }
                                ],
                                "Value": "106.50"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "568f2e9a-0870-46cc-8678-f83f132ed4e3"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "568f2e9a-0870-46cc-8678-f83f132ed4e3"
                                    }
                                ],
                                "Value": "106.50"
                            }
                        ],
                        "RowType": "Row"
                    },
                    {
                        "Cells": [
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "81f39534-8a2d-4181-8e1a-d9bca54bf7d6"
                                    }
                                ],
                                "Value": "GST (820)"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "81f39534-8a2d-4181-8e1a-d9bca54bf7d6"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "81f39534-8a2d-4181-8e1a-d9bca54bf7d6"
                                    }
                                ],
                                "Value": "128.84"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "81f39534-8a2d-4181-8e1a-d9bca54bf7d6"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "81f39534-8a2d-4181-8e1a-d9bca54bf7d6"
                                    }
                                ],
                                "Value": "748.25"
                            }
                        ],
                        "RowType": "Row"
                    },
                    {
                        "Cells": [
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "0f63cdaf-3f97-497e-8f28-405f0ea0c7a2"
                                    }
                                ],
                                "Value": "Historical Adjustment (840)"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "0f63cdaf-3f97-497e-8f28-405f0ea0c7a2"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "0f63cdaf-3f97-497e-8f28-405f0ea0c7a2"
                                    }
                                ],
                                "Value": "0.00"
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "0f63cdaf-3f97-497e-8f28-405f0ea0c7a2"
                                    }
                                ],
                                "Value": ""
                            },
                            {
                                "Attributes": [
                                    {
                                        "Id": "account",
                                        "Value": "0f63cdaf-3f97-497e-8f28-405f0ea0c7a2"
                                    }
                                ],
                                "Value": "4130.98"
                            }
                        ],
                        "RowType": "Row"
                    }
                ],
                "Title": "Liabilities"
            },
            {
                "RowType": "Section",
                "Rows": [
                    {
                        "Cells": [
                            {
                                "Value": "Total"
                            },
                            {
                                "Value": "1278.82"
                            },
                            {
                                "Value": "1278.82"
                            },
                            {
                                "Value": "10811.80"
                            },
                            {
                                "Value": "10811.80"
                            }
                        ],
                        "RowType": "SummaryRow"
                    }
                ],
                "Title": ""
            }
        ],
        "UpdatedDateUTC": "/Date(1597713249656+0000)/"
    }
]

}

我对编码很陌生,并且正在努力将其转换为 CSV。处理这个问题时,最好的起点是哪里?

标签: pythonjsonxero-api

解决方案


推荐阅读