-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_navigator.py
79 lines (69 loc) · 2.23 KB
/
json_navigator.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
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
70
71
72
73
74
75
76
77
78
79
'''a module to navigate through json'''
import json
def main():
'''
Main function.
'''
path = input('Enter the path to your json-file: ')
try:
with open(path, 'r') as file:
data = json.load(file)
except FileNotFoundError:
quit('No such file or directory! Try again..')
print(json_flatten(data, [data]))
def json_flatten(info, return_data):
'''
The function to navigate through json-file.
'''
if type(info) != list and type(info) != dict:
return info
# print(info)
# user_input = input('Want to return or end? ')
# if user_input == 'return':
# return_data.pop(-1)
# info1 = return_data[-1]
# return json_flatten(info1, return_data)
if len(info) == 1:
return info
elif type(info) == dict:
print(list(info.keys()))
user_input = input('Enter a key from list: ')
if user_input == 'back':
return_data.pop(-1)
if len(return_data) != 0:
info1 = return_data[-1]
return json_flatten(info1, return_data)
else:
return_data = [info]
return json_flatten(info, return_data)
if not user_input:
exit()
try:
info1 = info[user_input]
return_data.append(info1)
except KeyError:
return json_flatten(info, return_data)
return json_flatten(info1, return_data)
elif type(info) == list:
if len(info) == 1:
user_input = 0
else:
print(info)
user_input = input(f'enter a number from 0 to {len(info)-1}: ')
if user_input == '':
exit()
if user_input == 'back':
return_data.pop(-1)
if len(return_data) != 0:
info1 = return_data[-1]
return json_flatten(info1, return_data)
else:
return_data = [info]
return json_flatten(info, return_data)
try:
info1 = info[int(user_input)]
except IndexError:
return json_flatten(info, return_data)
return json_flatten(info1, return_data)
if __name__ == '__main__':
main()