import json
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def batch_user_data_operations(input_file, output_file, operations, dry_run=False):
"""
Performs batch operations on user data from a JSON file.
Args:
input_file (str): Path to the input JSON file containing user data.
output_file (str): Path to the output JSON file to save processed data.
operations (list): A list of dictionaries, where each dictionary
specifies an operation to perform on each user.
Example:
[
{"operation": "update", "field": "email", "value": "new_email@example.com"},
{"operation": "add_tag", "tag": "active"}
]
dry_run (bool): If True, the operations are simulated and no changes
are made to the output file.
"""
try:
with open(input_file, 'r') as f:
user_data = json.load(f)
except FileNotFoundError:
logging.error(f"Input file not found: {input_file}")
return
except json.JSONDecodeError:
logging.error(f"Invalid JSON format in: {input_file}")
return
processed_data = []
for user in user_data:
processed_user = user.copy() # Create a copy to avoid modifying original data
for op in operations:
operation_type = op['operation']
if operation_type == 'update':
field = op['field']
value = op['value']
processed_user[field] = value
elif operation_type == 'add_tag':
tag = op['tag']
if 'tags' not in processed_user:
processed_user['tags'] = []
if tag not in processed_user['tags']:
processed_user['tags'].append(tag)
elif operation_type == 'delete_tag':
tag = op['tag']
if 'tags' in processed_user and tag in processed_user['tags']:
processed_user['tags'].remove(tag)
elif operation_type == 'append_field':
field = op['field']
value = op['value']
processed_user[field] = value
else:
logging.warning(f"Unknown operation: {operation_type}")
processed_data.append(processed_user)
if not dry_run:
try:
with open(output_file, 'w') as f:
json.dump(processed_data, f, indent=4)
logging.info(f"Successfully processed data and saved to: {output_file}")
except IOError:
logging.error(f"Error writing to output file: {output_file}")
else:
logging.info("Dry-run mode: No changes made to the output file.")
if __name__ == '__main__':
# Example Usage
input_file = 'user_data.json'
output_file = 'processed_user_data.json'
# Sample user data (create a user_data.json file with this content)
sample_data = [
{"id": 1, "name": "Alice", "email": "alice@example.com", "tags": ["VIP"]},
{"id": 2, "name": "Bob", "email": "bob@example.com"},
{"id": 3, "name": "Charlie", "email": "charlie@example.com", "tags": ["regular"]}
]
with open(input_file, 'w') as f:
json.dump(sample_data, f, indent=4)
operations = [
{"operation": "update", "field": "email", "value": "alice.new@example.com"},
{"operation": "add_tag", "tag": "premium"},
{"operation": "append_field", "field": "city", "value": "New York"}
]
batch_user_data_operations(input_file, output_file, operations, dry_run=False)
Add your comment