Skip to content Skip to sidebar Skip to footer

Nested JSON To CSV Using Python Script

i'm new to python and I've got a large json file that I need to convert to csv - below is a sample { 'status': 'success','Name': 'Theresa May','Location': '87654321','AccountCatego

Solution 1:

This works for the JSON example you posted. The issue is that you have nested dict and you can't create sub-headers and sub rows for pcredit, pdebit, pbalance, ptransactiondate, pvaluedate and ptest as you want.

You can use csv.DictWriter:

import csv
import json

with open("BankStatementJSON1.json", "r") as inputFile:  # open json file
    data = json.loads(inputFile.read())  # load json content

with open("testing.csv", "w") as outputFile:  # open csv file
    output = csv.DictWriter(outputFile, data.keys())  # create a writer
    output.writeheader()
    output.writerow(data)

Solution 2:

Make sure you're closing the output file at the end as well.


Post a Comment for "Nested JSON To CSV Using Python Script"