Skip to content

Instantly share code, notes, and snippets.

@rbk
Last active April 9, 2019 17:02
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 rbk/18a0bab6036e4eed4307505a5567e861 to your computer and use it in GitHub Desktop.
Save rbk/18a0bab6036e4eed4307505a5567e861 to your computer and use it in GitHub Desktop.
Json to CSV
import sys
import json
input_file = sys.argv[len(sys.argv)-1]
print('INPUT: ', input_file)
output_file = input_file + '.csv'
data = json.loads(open(input_file).read())
# print(",".join(list(data[0].keys())))
csv_headers = ",".join(list(data[0].keys()))
with open(output_file, 'w') as f:
f.write(csv_headers + '\n')
for obj in data:
line = ""
values = []
for key in obj:
values.append(str(obj[key]))
line = ",".join(values)
# print(line)
f.write(line + '\n')
print(output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment