import json
def diagnose_json(json_data):
"""
Imports JSON data and provides simple error messages if parsing fails.
"""
try:
data = json.loads(json_data) # Parse JSON string to Python object
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}") # Print error message if parsing fails
return None # Return None if parsing fails
# Process the parsed JSON data here if needed.
# For example, you could access specific fields:
# if 'error' in data:
# print(f"Diagnostic error: {data['error']}")
return data # Return the parsed JSON data if successful
if __name__ == '__main__':
# Example Usage
valid_json = '{"status": "success", "message": "Diagnostics completed"}'
invalid_json = '{"status": "error", "message": "Invalid JSON" ' # Missing closing brace
diagnostics_data = diagnose_json(valid_json)
if diagnostics_data:
print("Diagnostics data received:")
print(diagnostics_data)
diagnostics_data = diagnose_json(invalid_json)
if diagnostics_data is None:
print("Diagnostics failed to process invalid JSON.")
Add your comment