Skip to content

Instantly share code, notes, and snippets.

@xbns
Created September 26, 2019 09:15
Show Gist options
  • Save xbns/c28820c24eca988b5c14c137998ddd7b to your computer and use it in GitHub Desktop.
Save xbns/c28820c24eca988b5c14c137998ddd7b to your computer and use it in GitHub Desktop.
#jsonTocsv #snippet
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