0

I am using python and I need to convert a nested JSON to CSV. I am exporting the data from Xero's API. I am unsure of how to deal with the nesting to get it into a readable output in CSV form. Here is the JSON I am dealing with:

{
"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)/"
    }
]

}

I am quiet new to coding and am struggling with dealing with converting this to CSV. Where is the best place to start when dealing with this?

  • isn't this your answer? https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv – TheFaultInOurStars Aug 18 '20 at 01:39
  • Does this answer your question? [How can I convert JSON to CSV?](https://stackoverflow.com/questions/1871524/how-can-i-convert-json-to-csv) – TheFaultInOurStars Aug 18 '20 at 01:39
  • CSVs are for tabular data. It looks like the `Rows` array has the tabular data, but it's not entirely straightforward. See if you can get documentation from Xero on the schema they use for their JSON. – Paul Bissex Aug 18 '20 at 01:41

0 Answers0