forked from Sarthak-kiloray/github-lab1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
25 lines (21 loc) · 953 Bytes
/
search.py
File metadata and controls
25 lines (21 loc) · 953 Bytes
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
import json
def search_json(json_data, search_string):
results = []
def search_in_data(data, search_string, path=""):
if isinstance(data, dict):
for key, value in data.items():
new_path = f"{path}/{key}" if path else key
if search_string.lower() in key.lower():
results.append(f"Found in key: {new_path}")
search_in_data(value, search_string, new_path)
elif isinstance(data, list):
for index, item in enumerate(data):
new_path = f"{path}[{index}]"
if isinstance(item, dict):
search_in_data(item, search_string, new_path)
else:
if search_string.lower() in str(data).lower():
results.append(f"Found in value at: {path}")
# Assuming json_data is a list of dictionaries
search_in_data(json_data, search_string)
return results