| import json | |
| def squash_details(data): | |
| """ | |
| Squashes details field into a single string with key-value pairs. | |
| Args: | |
| data: A list of dictionaries containing message and details fields. | |
| Returns: | |
| A list of dictionaries with the modified details field. | |
| """ | |
| for item in data: | |
| details_str = ", ".join([f"{key}: {value}" for key, value in item["details"].items()]) | |
| item["details"] = details_str | |
| return data | |
| # Read data from data.json | |
| with open("data.json", "r") as file: | |
| data = json.load(file) | |
| # Squash details | |
| squashed_data = squash_details(data) | |
| # Write modified data to data2.json | |
| with open("data2.json", "w") as file: | |
| json.dump(squashed_data, file, indent=4) # Add indentation for readability (optional) | |
| print("Successfully processed data and wrote to data2.json!") | |