Skip to content

Instantly share code, notes, and snippets.

@Mr-Perfection
Created January 4, 2021 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mr-Perfection/4351e696abff83b8be9063593aecb650 to your computer and use it in GitHub Desktop.
Save Mr-Perfection/4351e696abff83b8be9063593aecb650 to your computer and use it in GitHub Desktop.
Convert .JSON file to .csv file
import json
import csv
import sys
def process():
arguments = sys.argv
if len(arguments) != 2:
print('please pass your json file as argument i.e. python json_to_csv.py [FILENAME].json')
return
json_file = arguments[1]
filename = json_file.split('.')[0]
with open(json_file) as json_file:
data = json.load(json_file)
# now we will open a file for writing
data_file = open(filename+'.csv', 'w')
# create the csv writer object
csv_writer = csv.writer(data_file)
count = 0
keys = data.keys()
for key in keys:
if count == 0:
# Writing headers of CSV file
csv_writer.writerow(["email", "name"])
count += 1
# Writing data of CSV file
csv_writer.writerow([key, data[key]])
data_file.close()
process()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment