Spaces:
Paused
Paused
| import json | |
| # Function to convert $oid to a string and clean up the data | |
| def process_data(input_file, output_file): | |
| with open(input_file, "r") as infile: | |
| data = json.load(infile) # Read the JSON file | |
| # Transform the data | |
| for record in data: | |
| if isinstance(record.get("_id"), dict) and "$oid" in record["_id"]: | |
| record["id"] = record["_id"]["$oid"] # Replace with string value | |
| del record["_id"] | |
| if "__v" in record: # Optionally remove the "__v" field | |
| del record["__v"] | |
| # Write the transformed data back to a new JSON file | |
| with open(output_file, "w") as outfile: | |
| json.dump(data, outfile, indent=4) # Pretty print with indent | |
| # File paths | |
| input_file = "./sampleDb.products.json" | |
| output_file = "./products.json" | |
| # Process the data | |
| process_data(input_file, output_file) | |
| print(f"Processed data written to {output_file}") | |