1. import json
  2. def extract_config(config_file):
  3. """
  4. Extracts configuration values from a JSON file.
  5. Args:
  6. config_file (str): Path to the JSON configuration file.
  7. Returns:
  8. dict: A dictionary containing the configuration values.
  9. Returns an empty dictionary if the file is not found or invalid.
  10. """
  11. try:
  12. with open(config_file, 'r') as f:
  13. config = json.load(f) # Load JSON data
  14. return config
  15. except FileNotFoundError:
  16. print(f"Error: Configuration file not found: {config_file}")
  17. return {}
  18. except json.JSONDecodeError:
  19. print(f"Error: Invalid JSON format in {config_file}")
  20. return {}
  21. if __name__ == '__main__':
  22. # Example usage: Replace 'config.json' with your actual config file
  23. config_values = extract_config('config.json')
  24. if config_values:
  25. # Print the extracted configuration values (for testing)
  26. print(json.dumps(config_values, indent=4))

Add your comment