-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.py
31 lines (26 loc) · 1.04 KB
/
parsing.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
def get_element_via_path(json_obj, path, ignore_nodes=[]):
"""
path = 'key1/0/key2'
Can be used to get a deeply nested element or node in a JSON data tree
returns is_found, element
ignore_nodes defines for which elements in path not to send a notification
"""
def get_element(json_obj, path):
path_items = path.strip('/').split("/")
first_item = path_items.pop(0)
try:
if first_item.isdigit():
json_obj = json_obj[int(first_item)]
else:
json_obj = json_obj[first_item]
except (AttributeError, KeyError) as err:
if first_item not in ignore_nodes:
print(f"Node fetching error: {first_item} missing in {full_path}")
return None
if path_items:
path = "/".join(path_items)
return get_element(json_obj, path)
else:
return json_obj
full_path = path # we define full path here which will be used in error handling
return get_element(json_obj, path)