-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2csv.py
141 lines (110 loc) · 3.06 KB
/
json2csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from collections import defaultdict
import csv
import json
import sys
import time
import pprint
import uuid
import configparser
try:
config = configparser.ConfigParser()
config.read('config.ini')
rootColumn=config['DEFAULT']['rootColumn']
parentColumn=config['DEFAULT']['parentColumn']
nodeColumn=config['DEFAULT']['nodeColumn']
subItemEscapeChar=config['DEFAULT']['subItemEscapeChar']
simpleListEscapeChar=config['DEFAULT']['simpleListEscapeChar']
booleanEscapeChar=config['DEFAULT']['booleanEscapeChar']
except:
print("Cannot read config file")
sys.exit(0)
if len(sys.argv)<2:
print("Usage: josn2csv.py <file.json>")
sys.exit(0)
elif len(sys.argv)==2:
file = sys.argv[1]
csv_data=[]
keys= []
jsonData={}
with open(file) as json_data:
d = json.load(json_data)
#Check if the input JSON is an array
if type(d)==type(list()):
jsonData[rootColumn]=d
#print(jsonData)
else:
jsonData=d
def getKeys(data):
global keys
if type(data) == type(dict()):
for key,value in data.items():
if key not in keys:
keys.append(key)
getKeys(data[key])
elif type(data) == type(list()):
for item in data:
getKeys(item)
def isListsimple(aList):
ans = True
for item in aList:
if type(item) in (type(list()), type(tuple()), type(dict())):
ans = False
return ans
def traverse(data, parent, current):
global csv_data
global keys
node = {}
node[parentColumn]= parent
node[nodeColumn]= current
if type(data) == type(list()):
for item in data:
traverse(item, parent, uuid.uuid4())
else:
for key in keys:
node[key]=''
if key in data:
if type(data[key]) != type(dict()) and type(data[key]) != type(list()) :
if type(data[key]) == type(bool()):
node[key]=booleanEscapeChar + str(data[key])+ booleanEscapeChar
else:
node[key]=data[key]
elif type(data[key]) == type(list()):
if isListsimple(data[key]):
node[key] =simpleListEscapeChar+json.dumps(data[key])+simpleListEscapeChar
else:
node[key]= subItemEscapeChar+key+subItemEscapeChar+"list"+subItemEscapeChar
elif type(data[key]) == type(dict()):
node[key]= subItemEscapeChar+key+subItemEscapeChar+"dict"+subItemEscapeChar
csv_data.append(node)
for key in keys:
try:
if type(data[key]) == type(dict()):
traverse(data[key], current, uuid.uuid4())
elif type(data[key]) == type(list()):
if not isListsimple(data[key]):
for item in data[key]:
traverse(item, current, uuid.uuid4())
except:
pass
getKeys(jsonData)
#print(keys)
traverse(jsonData, None, uuid.uuid4())
outFile = file +'_'+ str(int(time.time()))+'.csv'
write_header = True
item_keys = []
with open(outFile, 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
for item in csv_data:
item_values = []
for key in item:
if write_header:
item_keys.append(key)
value = item.get(key, '')
if isinstance(value, str):
item_values.append(value)
else:
item_values.append(value)
if write_header:
writer.writerow(item_keys)
write_header = False
writer.writerow(item_values)