File size: 807 Bytes
bcffb9c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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!")
|