1. import json
  2. import logging
  3. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  4. def batch_user_data_operations(input_file, output_file, operations, dry_run=False):
  5. """
  6. Performs batch operations on user data from a JSON file.
  7. Args:
  8. input_file (str): Path to the input JSON file containing user data.
  9. output_file (str): Path to the output JSON file to save processed data.
  10. operations (list): A list of dictionaries, where each dictionary
  11. specifies an operation to perform on each user.
  12. Example:
  13. [
  14. {"operation": "update", "field": "email", "value": "new_email@example.com"},
  15. {"operation": "add_tag", "tag": "active"}
  16. ]
  17. dry_run (bool): If True, the operations are simulated and no changes
  18. are made to the output file.
  19. """
  20. try:
  21. with open(input_file, 'r') as f:
  22. user_data = json.load(f)
  23. except FileNotFoundError:
  24. logging.error(f"Input file not found: {input_file}")
  25. return
  26. except json.JSONDecodeError:
  27. logging.error(f"Invalid JSON format in: {input_file}")
  28. return
  29. processed_data = []
  30. for user in user_data:
  31. processed_user = user.copy() # Create a copy to avoid modifying original data
  32. for op in operations:
  33. operation_type = op['operation']
  34. if operation_type == 'update':
  35. field = op['field']
  36. value = op['value']
  37. processed_user[field] = value
  38. elif operation_type == 'add_tag':
  39. tag = op['tag']
  40. if 'tags' not in processed_user:
  41. processed_user['tags'] = []
  42. if tag not in processed_user['tags']:
  43. processed_user['tags'].append(tag)
  44. elif operation_type == 'delete_tag':
  45. tag = op['tag']
  46. if 'tags' in processed_user and tag in processed_user['tags']:
  47. processed_user['tags'].remove(tag)
  48. elif operation_type == 'append_field':
  49. field = op['field']
  50. value = op['value']
  51. processed_user[field] = value
  52. else:
  53. logging.warning(f"Unknown operation: {operation_type}")
  54. processed_data.append(processed_user)
  55. if not dry_run:
  56. try:
  57. with open(output_file, 'w') as f:
  58. json.dump(processed_data, f, indent=4)
  59. logging.info(f"Successfully processed data and saved to: {output_file}")
  60. except IOError:
  61. logging.error(f"Error writing to output file: {output_file}")
  62. else:
  63. logging.info("Dry-run mode: No changes made to the output file.")
  64. if __name__ == '__main__':
  65. # Example Usage
  66. input_file = 'user_data.json'
  67. output_file = 'processed_user_data.json'
  68. # Sample user data (create a user_data.json file with this content)
  69. sample_data = [
  70. {"id": 1, "name": "Alice", "email": "alice@example.com", "tags": ["VIP"]},
  71. {"id": 2, "name": "Bob", "email": "bob@example.com"},
  72. {"id": 3, "name": "Charlie", "email": "charlie@example.com", "tags": ["regular"]}
  73. ]
  74. with open(input_file, 'w') as f:
  75. json.dump(sample_data, f, indent=4)
  76. operations = [
  77. {"operation": "update", "field": "email", "value": "alice.new@example.com"},
  78. {"operation": "add_tag", "tag": "premium"},
  79. {"operation": "append_field", "field": "city", "value": "New York"}
  80. ]
  81. batch_user_data_operations(input_file, output_file, operations, dry_run=False)

Add your comment