forked from WhitmanCSCapstone/bmac-warehouse-utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_json.py
More file actions
69 lines (53 loc) · 1.99 KB
/
fix_json.py
File metadata and controls
69 lines (53 loc) · 1.99 KB
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
import glob
import json
import csv
import os
# get json files
json_files = glob.glob("./stub_json/*.json")
# clean up the names
file_names = []
for name in json_files:
file_names.append(name[12:-5])
def parseItems(data, table_name, accessor):
"""function that takes a json object, a table name, and an accessor key and parses the items from strings to arrays of json objects"""
entries = data[table_name.lower()]
for entry in entries:
# edge case
if(entry[accessor] == ''):
entry[accessor] = []
continue
items = entry[accessor].split(",")
new_items = []
for item in items:
# product: case lots: tot weight
values = item.split(":")
keys = ["product", "case_lots", "total_weight"]
# if no data except for product name
if(":" not in item):
keys = ["product"]
# product; unit weight: case lots: total weight
if(";" in values[0]):
values = values[0].split(';') + values[1:3]
keys = ['product', 'unit_weight', 'case_lots', 'total_weight']
# create the new item as a dict
json_dict = {keys[i] : values [i] for i in range(len(keys))}
new_items.append(json_dict)
entry[accessor] = new_items
data[table_name.lower()] = entries
return data
for json_file in json_files:
clean_name = json_file[12:-5]
if clean_name == "Shipments":
file = open(json_file, 'r')
data = json.loads(file.read())
file.close()
new_data = parseItems(data, clean_name, "ship_items")
with open(json_file, 'w') as outfile:
json.dump(new_data, outfile)
elif clean_name == "Contributions":
file = open(json_file, 'r')
data = json.loads(file.read())
file.close()
new_data = parseItems(data, clean_name, "receive_items")
with open(json_file, 'w') as outfile:
json.dump(new_data, outfile)