1. import json
  2. import logging
  3. # Configure logging
  4. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  5. def strip_metadata_from_form_fields(form_data):
  6. """
  7. Strips metadata from form fields. Handles potential errors gracefully.
  8. Args:
  9. form_data (dict): A dictionary representing form data.
  10. Returns:
  11. dict: A new dictionary with metadata stripped, or None if an error occurred.
  12. """
  13. try:
  14. stripped_data = {}
  15. for key, value in form_data.items():
  16. if isinstance(value, dict): # Check if the value is a dictionary (metadata)
  17. stripped_value = {}
  18. for metadata_key, metadata_value in value.items():
  19. stripped_value[metadata_key] = None # Set metadata to None
  20. stripped_data[key] = stripped_value
  21. else:
  22. stripped_data[key] = value
  23. return stripped_data
  24. except Exception as e:
  25. logging.error(f"Error stripping metadata: {e}")
  26. return None
  27. if __name__ == '__main__':
  28. # Example Usage
  29. form_data = {
  30. "name": "John Doe",
  31. "email": "john.doe@example.com",
  32. "address": {
  33. "street": "123 Main St",
  34. "city": "Anytown",
  35. "zip": "12345",
  36. "metadata": {
  37. "source": "website",
  38. "timestamp": "2023-10-27T10:00:00Z"
  39. }
  40. },
  41. "phone": "555-123-4567"
  42. }
  43. stripped_form_data = strip_metadata_from_form_fields(form_data)
  44. if stripped_form_data:
  45. print(json.dumps(stripped_form_data, indent=4))
  46. else:
  47. print("Metadata stripping failed.")

Add your comment