-
Notifications
You must be signed in to change notification settings - Fork 5
/
tests_convert_strigs.py
42 lines (34 loc) · 1.36 KB
/
tests_convert_strigs.py
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
31
32
33
34
35
36
37
38
39
40
41
42
import os
import json
# Hardcoded input folder and output file paths
input_folder = "./tests/string" # Replace with the actual path to your folder
output_file = "./tests_string.json"
def merge_json_files(input_folder, output_file):
combined_data = []
for index in range(12):
filename = f"test_{index}.json"
filepath = os.path.join(input_folder, filename)
if os.path.exists(filepath): # Check if the file exists
try:
# Open and parse the JSON file
with open(filepath, "r") as file:
data = json.load(file)
# Extract only the required properties
entry = {
"body": data.get("body", []),
"string": data.get("string", [])
}
# Add to the combined data list
combined_data.append(entry)
except Exception as e:
print(f"Error processing file {filename}: {e}")
else:
print(f"File {filename} does not exist. Skipping.")
try:
with open(output_file, "w") as outfile:
json.dump(combined_data, outfile, indent=2)
print(f"Successfully written to {output_file}")
except Exception as e:
print(f"Error writing output file: {e}")
# Run the function
merge_json_files(input_folder, output_file)