1. import json
  2. def index_config_values(config_files):
  3. """
  4. Indexes configuration values from multiple JSON files.
  5. Args:
  6. config_files: A list of paths to JSON configuration files.
  7. Returns:
  8. A dictionary where keys are configuration keys and values are lists of
  9. values found in the configuration files.
  10. """
  11. all_values = {}
  12. for config_file in config_files:
  13. try:
  14. with open(config_file, 'r') as f:
  15. config = json.load(f)
  16. except FileNotFoundError:
  17. print(f"Error: Config file not found: {config_file}")
  18. continue # Skip to the next file
  19. except json.JSONDecodeError:
  20. print(f"Error: Invalid JSON in file: {config_file}")
  21. continue # Skip to the next file
  22. for key, value in config.items():
  23. if key not in all_values:
  24. all_values[key] = []
  25. all_values[key].append(value)
  26. return all_values
  27. if __name__ == '__main__':
  28. # Example Usage
  29. config_files = ["config1.json", "config2.json"] # Replace with your file paths
  30. # Create dummy config files for testing
  31. with open("config1.json", "w") as f:
  32. json.dump({"key1": "value1", "key2": 123, "key3": True}, f)
  33. with open("config2.json", "w") as f:
  34. json.dump({"key1": "value2", "key4": 456, "key2": 456}, f)
  35. indexed_values = index_config_values(config_files)
  36. print(json.dumps(indexed_values, indent=2))

Add your comment