-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_to_csv.py
87 lines (64 loc) · 2.83 KB
/
json_to_csv.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
import os
import sys
import csv
import json
from collections import defaultdict
# value_types = [str, float, int]
def read_file(file_to_read):
data = []
counter = 0
with open(file_to_read, encoding='utf-16') as file:
for line in file:
if counter == 0 and not line.startswith("["):
line = "[" + line
counter += 1
data.append(line)
data.append("]") if not data[-1].endswith(']') else None
return data
if len(sys.argv) == 1:
sys.exit('\n \n No input file name given as parameter \n')
else:
files = []
if len(sys.argv) == 2 and sys.argv[1] == "all":
contents = os.listdir(os.path.dirname(os.path.abspath(__file__)))
for item in contents:
if item.endswith(".json") or item.endswith(".txt"):
files.append(item)
elif len(sys.argv) >= 2:
files = [item for item in sys.argv[1:] if
os.path.isfile(os.path.join(os.path.dirname(os.path.abspath(__file__)), item)) and (
item.endswith('.json') or item.endswith(".txt"))]
sys.exit('No file present') if len(files) == 0 else None
for input_file in files:
print("\n Processing file : ", input_file)
data = read_file(input_file)
str_data = ""
for i in data:
if not i == "\n":
str_data += i
if "}\n{" in str_data:
str_data = str_data.replace("}\n{", "},\n{")
if "} {" in str_data:
str_data = str_data.replace("} {", "},\n{")
json_data = json.loads(str_data)
dict_objects_list = []
# currently only for one level down the top level eg: {"name": "john", "phone" :{"mobile": XXXX, "home": ZZZZ } }
for item in json_data:
if isinstance(item, dict):
per_object_dict = defaultdict(str)
for key, value in item.items():
if not isinstance(value, dict):
per_object_dict[key] = str(value)
elif isinstance(value, dict):
for inner_key, inner_value in value.items():
per_object_dict[key] = str(inner_value)
dict_objects_list.append(per_object_dict)
keys = set(key for key in dict_objects_list[0].keys())
# print('\n \n keys : \n', sorted(keys))
data = [[obj_dict[key] for key in sorted(keys)] for obj_dict in dict_objects_list]
with open(input_file.split(".")[0] + '_output.csv', 'w', encoding='utf-8', newline='') as output:
writer = csv.writer(output, delimiter=',')
header = [key for key in sorted(keys)]
writer.writerow(header)
for item in data:
writer.writerow(item)