Created
September 26, 2019 09:15
-
-
Save xbns/c28820c24eca988b5c14c137998ddd7b to your computer and use it in GitHub Desktop.
#jsonTocsv #snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import sys | |
from flatten_json import flatten | |
import pandas as pd | |
#check if you pass the input file | |
if sys.argv[1] is not None: | |
fileInput = sys.argv[1] | |
inputFile = open(fileInput) #open json file | |
data = json.load(inputFile) #load json content | |
flattened = (flatten(d) for d in data) | |
inputFile.close() #close the input file | |
# for item in flattened.items(): | |
# print(item) | |
# print(type(item)) | |
# print(type(flattened)) | |
df = pd.DataFrame(flattened) | |
print(df) | |
df.to_csv('output.csv',index=False) | |
""" | |
Usage | |
$ python jsonTocsv.py input.json | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment