Created
December 1, 2015 22:01
-
-
Save kevinschaul/4736d32c33445ebd5b88 to your computer and use it in GitHub Desktop.
Convert a .json file into a CSV file, flattening any embedded objects and arrays
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
#!/usr/bin/evn python | |
import csv | |
import json | |
import sys | |
from sets import Set | |
def main(): | |
""" | |
Convert a .json file into a CSV file, flattening any embedded objects and | |
arrays | |
""" | |
if len(sys.argv) != 3: | |
print('USAGE: python flattenIn2csv.py JSON_FILE KEY') | |
sys.exit(1) | |
in_file = sys.argv[1] | |
key = sys.argv[2] | |
data = {} | |
with open(in_file) as in_f: | |
data = json.loads(in_f.read()) | |
keys = Set() | |
flattened = [] | |
for row in data[key]: | |
flat = flattenDict(row) | |
flattened.append(flat) | |
keys.update(flat.keys()) | |
csv_writer = csv.DictWriter(sys.stdout, keys) | |
csv_writer.writeheader() | |
for row in flattened: | |
csv_writer.writerow(row) | |
def flattenDict(d, result=None): | |
""" | |
https://gist.github.com/higarmi/6708779 | |
""" | |
if result is None: | |
result = {} | |
for key in d: | |
value = d[key] | |
if isinstance(value, dict): | |
value1 = {} | |
for keyIn in value: | |
value1[".".join([key,keyIn])]=value[keyIn] | |
flattenDict(value1, result) | |
elif isinstance(value, (list, tuple)): | |
for indexB, element in enumerate(value): | |
if isinstance(element, dict): | |
value1 = {} | |
index = 0 | |
for keyIn in element: | |
newkey = ".".join([key,keyIn]) | |
value1[".".join([key,keyIn])]=value[indexB][keyIn] | |
index += 1 | |
for keyA in value1: | |
flattenDict(value1, result) | |
else: | |
result[key]=value | |
return result | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it is working but , incase if there is a dictionary inside a list then previous keys were getting overwritten by last key .
"records":[
{"_app_type":"PREGNANT", /* getting overwritten by below entry */
"_data_type":"DAYS_POST"
},
{"_app_type":"VISION",
"_data_type":"DAYS_PRE"
}]
only _app_type":"VISION is being shown up not the firs one in above case