File size: 1,118 Bytes
90cd92a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import random
import argparse
import os

def shuffle_json(input_file, output_file):
    # Read the JSON file
    with open(input_file, 'r') as f:
        data = json.load(f)

    # Ensure the data is a list
    if not isinstance(data, list):
        raise ValueError("JSON data is not a list")

    # Shuffle the data
    random.shuffle(data)

    # Write the shuffled data to the JSON file
    with open(output_file, 'w') as f:
        json.dump(data, f, indent=4)

def main():
    # Set up argument parsing
    parser = argparse.ArgumentParser(description='Shuffle a JSON file.')
    parser.add_argument('input_file', help='The input JSON file to shuffle.')
    parser.add_argument('output_file', nargs='?', default=None, help='The output JSON file. If not provided, the input file will be overwritten.')

    args = parser.parse_args()

    # Use the same file for input and output if output_file is not provided
    output_file = args.output_file if args.output_file else args.input_file

    # Shuffle the JSON file
    shuffle_json(args.input_file, output_file)

if __name__ == "__main__":
    main()