-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSON_PARSER.py
More file actions
43 lines (35 loc) · 1.21 KB
/
JSON_PARSER.py
File metadata and controls
43 lines (35 loc) · 1.21 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
import json
class JSONParser(object):
def __init__(self, json_file):
self.json_file = json_file
self.data = {}
def parse_dict_or_list(self, key, d):
if isinstance(d, list):
for sub_dict in d:
if key in sub_dict:
return sub_dict[key]
return None
else:
if key in d:
return d[key]
else:
None
def make_query(self, query_list):
working_level = self.data
for item in query_list:
working_level = self.parse_dict_or_list(item, working_level)
if working_level == None:
return None
return working_level
def load_json(self):
with open(self.json_file) as data_file:
self.data = json.load(data_file)
def query(self, query_string):
self.load_json()
result = self.make_query(query_string.split('/'))
return result
p = JSONParser('userdata.json')
result = p.query('ABC/DEF/OverAllHealth/CPUs/Status/MemorySummary/TotalSystemMemoryGiB')
print(result)
result = p.query("ABC/DEF/OverAllHealth/PowerSupplies/Status")
print(result)