-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsearch.py
28 lines (24 loc) · 934 Bytes
/
jsearch.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
import json
config = "config.json"
def getkey(file_name,key_string):
with open(file_name,"r")as file:
data = json.load(file)
keys = key_string.split('.')
for index, key in enumerate(keys):
if key.isdigit() and isinstance(data,list):
list_index = int(key)
if list_index < len(data):
data = data[list_index]
else:
print("Search failed wrong index: ",list_index,"in key string: ",end=" ")
for i in range(index + 1):
print(keys[i],end=" ")
return
elif isinstance(data,dict) and key in data:
data = data[key]
else:
print("Search failed wrong key: ",end="")
for i in range(index + 1):
print(keys[i])
return
return data