Mercurial > repos > cpt > cpt_search_file
comparison explodeJSON.py @ 1:6e3a843b6304 draft
planemo upload commit 94b0cd1fff0826c6db3e7dc0c91c0c5a8be8bb0c
author | cpt |
---|---|
date | Mon, 05 Jun 2023 02:53:18 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:bd2ff2c7e806 | 1:6e3a843b6304 |
---|---|
1 import json | |
2 | |
3 | |
4 class explodeJSON: | |
5 def __init__(self, file): | |
6 self.file = file | |
7 | |
8 def readJSON(self): | |
9 """returns dictionary object for reading a JSON""" | |
10 with open(self.file) as j: | |
11 myObj = json.load(j) | |
12 | |
13 return myObj | |
14 | |
15 def explode(self): | |
16 """Makes a list of each embedded list from the database JSON""" | |
17 | |
18 data = self.readJSON() | |
19 | |
20 terms = [] | |
21 for v in data.values(): | |
22 for term in v: | |
23 terms.append(term) | |
24 | |
25 return terms | |
26 | |
27 | |
28 ### Dictionary Functions | |
29 def save_dict_to_json(obj, filename="output.json"): | |
30 with open(filename, "w") as js: | |
31 print("saved {} as json".format(filename)) | |
32 json.dump(obj, js, indent=4) | |
33 | |
34 | |
35 if __name__ == "__main__": | |
36 query = [] | |
37 filepath = "test-data/" | |
38 filename = "test.json" | |
39 e = explodeJSON(file=filepath + filename) | |
40 data = e.readJSON() | |
41 print(data) | |
42 for k, v in data.items(): | |
43 for term in v: | |
44 print(k + ":" + term) # print global term to synonym / children terms. | |
45 | |
46 print("++ ========= ++") | |
47 | |
48 terms = e.explode() | |
49 print(terms) | |
50 | |
51 test = {"math": ["algebra", "calculus"]} | |
52 print(type(test)) | |
53 save_dict_to_json(obj=test, filename="test-output.json") |