in check_docs_version_diff.py [0:0]
def extract_items_from_file(file_path):
"""Read the JSON file, extract 'items', and filter them based on exclusion criteria."""
result = [] # List to store the filtered items
try:
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file) # Load the JSON data
# Recursive function to extract items and filter them
def extract_items(data):
"""Recursively extract 'items' and store them in result, excluding specified keywords."""
if isinstance(data, list):
for item in data:
extract_items(item) # Process each item in the list
elif isinstance(data, dict):
if "items" in data:
# Add valid items (not containing excluded keywords)
result.extend([item for item in data["items"] if isinstance(item, str) and not any(keyword in item for keyword in exclude_keywords)])
# Recursively process each key in the dictionary
for key in data:
extract_items(data[key])
extract_items(data) # Start extracting items from the loaded JSON data
return result # Return the list of filtered items
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error: {e}")
return []
except Exception as e:
print(f"An unexpected error occurred: {e}")
return []