File size: 1,473 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
import json
import os

def convert_json_file(original_file_path, new_file_path):
    # Load the original JSON file
    with open(original_file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    
    # Split the data
    if len(data) > 1:
        train_data = data[:-1]
        test_data = [data[-1]]
    else:
        # Handle case where there's only one item in the array
        train_data = []
        test_data = data
    
    # Prepare the new structure
    new_data = {
        "train": train_data,
        "test": test_data
    }
    
    # Write the new JSON file
    with open(new_file_path, 'w', encoding='utf-8') as file:
        json.dump(new_data, file, ensure_ascii=False, indent=4)

def convert_all_json_files_in_directory(directory_path, output_directory_path):
    # Ensure output directory exists
    if not os.path.exists(output_directory_path):
        os.makedirs(output_directory_path)

    # Iterate over all files in the directory
    for filename in os.listdir(directory_path):
        if filename.endswith(".json"):
            original_file_path = os.path.join(directory_path, filename)
            new_file_path = os.path.join(output_directory_path, filename)
            convert_json_file(original_file_path, new_file_path)
            print(f"Converted {filename}")

# Example usage
directory_path = 'output_a'
output_directory_path = 'output_b'
convert_all_json_files_in_directory(directory_path, output_directory_path)