-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_handler.py
More file actions
61 lines (55 loc) · 2.35 KB
/
json_handler.py
File metadata and controls
61 lines (55 loc) · 2.35 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
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
import os
import json
import config
import logging
def load_json_files(directory):
"""
Load all JSON files from a directory and return their contents as a list.
Each file should contain an array of objects.
"""
json_data = []
try:
if not os.path.exists(directory):
logging.warning(f"Directory {directory} does not exist.")
return []
for filename in os.listdir(directory):
if filename.endswith(".json"):
file_path = os.path.join(directory, filename)
try:
with open(file_path, "r") as file:
file_data = json.load(file)
# Add source filename for debugging
if isinstance(file_data, dict):
file_data['_source_file'] = filename
json_data.append(file_data)
elif isinstance(file_data, list):
for item in file_data:
if isinstance(item, dict):
item['_source_file'] = filename
json_data.extend(file_data)
else:
logging.warning(f"Unexpected JSON structure in {filename}")
except Exception as e:
logging.error(f"Error loading JSON from {filename}: {e}")
logging.info(f"Loaded {len(json_data)} items from {directory}")
return json_data
except Exception as e:
logging.error(f"Error in load_json_files: {e}")
return []
def get_json_value(json_data, key, default="N/A"):
"""Safely retrieve a key value from JSON."""
if not isinstance(json_data, dict):
return default
return json_data.get(key, default)
def find_matching_item_by_url(url, json_data_list):
"""Find a matching item in the JSON data list based on URL."""
for item in json_data_list:
if get_json_value(item, "url") == url:
return item
return None
def find_matching_rate_by_product(product_name, rates_list):
"""Find a matching rate in the rates list based on product name."""
for rate_item in rates_list:
if get_json_value(rate_item, "product_name") == product_name:
return get_json_value(rate_item, "rate")
return "N/A"