-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_parser_program.py
More file actions
36 lines (30 loc) · 1.1 KB
/
json_parser_program.py
File metadata and controls
36 lines (30 loc) · 1.1 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
'''
Parser of json files.
'''
def find_value(json_content: dict, user_key: str):
'''
Returns a value from the field given by a user.
'''
def investigate(section, key):
'''
Returns a value corresponding to a given key
or (False, None) if the given key wasn't found.
'''
if isinstance(section, list):
for sub_element in section:
investigation_result, discovered_value = investigate(sub_element, key)
if investigation_result:
return (True, discovered_value)
elif isinstance(section, dict):
if key in section:
return (True, section[key])
for sub_section_key in section:
investigation_result, discovered_value = investigate(section[sub_section_key], key)
if investigation_result:
return (True, discovered_value)
return (False, None)
investigation_conclusion = investigate(json_content, user_key)
return investigation_conclusion
if __name__ == '__main__':
import doctest
doctest.testmod()