| 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() |