![Python write json to csv](https://cdn1.cdnme.se/5447227/9-3/screenshot_5_64e62947ddf2b306ca76f796.jpg)
![python write json to csv python write json to csv](https://i.stack.imgur.com/cR0Cu.png)
Hi everybody, this is a simple snippet to help you convert your JSON file to a CSV file using a Python script.Ĭreate a new Python file like: json_to_csv.pyĪdd this code: import csv, json, sys #if you are not using utf-8 files, remove the next line sys.setdefaultencoding("UTF-8") #set the encode to utf8 #check if you pass the input file and output file if sys.argv is not None and sys.argv is not None: fileInput = sys.argv fileOutput = sys.argv inputFile = open(fileInput) #open json file outputFile = open(fileOutput, 'w') #load csv file data = json.load(inputFile) #load json content inputFile.close() #close the input file output = csv.writer(outputFile) #create a csv.write output.writerow(data.keys()) # header row for row in data: output.writerow(row.values()) #values rowĪfter adding this, save the file and run at the terminal:
![python write json to csv python write json to csv](https://tablecruncher.com/blog/tutorial/csv-to-json/csv-json-02.png)
How to convert a JSON file to CSV - PYTHON SCRIPT
![Python write json to csv](https://cdn1.cdnme.se/5447227/9-3/screenshot_5_64e62947ddf2b306ca76f796.jpg)