import json
def index_config_values(config_files):
"""
Indexes configuration values from multiple JSON files.
Args:
config_files: A list of paths to JSON configuration files.
Returns:
A dictionary where keys are configuration keys and values are lists of
values found in the configuration files.
"""
all_values = {}
for config_file in config_files:
try:
with open(config_file, 'r') as f:
config = json.load(f)
except FileNotFoundError:
print(f"Error: Config file not found: {config_file}")
continue # Skip to the next file
except json.JSONDecodeError:
print(f"Error: Invalid JSON in file: {config_file}")
continue # Skip to the next file
for key, value in config.items():
if key not in all_values:
all_values[key] = []
all_values[key].append(value)
return all_values
if __name__ == '__main__':
# Example Usage
config_files = ["config1.json", "config2.json"] # Replace with your file paths
# Create dummy config files for testing
with open("config1.json", "w") as f:
json.dump({"key1": "value1", "key2": 123, "key3": True}, f)
with open("config2.json", "w") as f:
json.dump({"key1": "value2", "key4": 456, "key2": 456}, f)
indexed_values = index_config_values(config_files)
print(json.dumps(indexed_values, indent=2))
Add your comment