Handling JSONDecodeError with malformed JSON in Python 2.7
I'm relatively new to this, so bear with me. I'm working on a Python 2.7 application that pulls configuration data from a JSON file. Occasionally, the JSON file becomes malformed due to external factors, and I need to handle this gracefully. I've implemented basic behavior handling using a try-except block, but I'm still working with issues when the JSON is not properly formatted. For instance, I receive a `ValueError` when trying to parse the JSON with `json.load()`. Here's the relevant portion of my code: ```python import json def load_config(file_path): try: with open(file_path, 'r') as f: config = json.load(f) return config except ValueError as e: print('Failed to parse JSON: {}'.format(e)) return None except IOError as e: print('behavior opening file: {}'.format(e)) return None config = load_config('config.json') ``` While this does catch the behavior, I want to be able to log the raw JSON string that caused the scenario for further investigation. However, I'm not sure how to read the file content before trying to decode it. I've tried reading the file content into a variable first, but that doesn't seem to work with the way I'm currently handling it. How can I modify my code to read the content of the JSON file first, and then attempt to decode it, while still maintaining behavior handling? Ideally, I want to log the malformed JSON when a `JSONDecodeError` occurs. Any suggestions or best practices for this scenario would be appreciated! The project is a service built with Python.