import json
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def strip_metadata_from_form_fields(form_data):
"""
Strips metadata from form fields. Handles potential errors gracefully.
Args:
form_data (dict): A dictionary representing form data.
Returns:
dict: A new dictionary with metadata stripped, or None if an error occurred.
"""
try:
stripped_data = {}
for key, value in form_data.items():
if isinstance(value, dict): # Check if the value is a dictionary (metadata)
stripped_value = {}
for metadata_key, metadata_value in value.items():
stripped_value[metadata_key] = None # Set metadata to None
stripped_data[key] = stripped_value
else:
stripped_data[key] = value
return stripped_data
except Exception as e:
logging.error(f"Error stripping metadata: {e}")
return None
if __name__ == '__main__':
# Example Usage
form_data = {
"name": "John Doe",
"email": "john.doe@example.com",
"address": {
"street": "123 Main St",
"city": "Anytown",
"zip": "12345",
"metadata": {
"source": "website",
"timestamp": "2023-10-27T10:00:00Z"
}
},
"phone": "555-123-4567"
}
stripped_form_data = strip_metadata_from_form_fields(form_data)
if stripped_form_data:
print(json.dumps(stripped_form_data, indent=4))
else:
print("Metadata stripping failed.")
Add your comment