import json def split_json(input_file, output_prefix, num_chunks): # Open the large JSON file with open(input_file, 'r') as file: data = json.load(file) # Get the list of keys (to split them into chunks) keys = list(data.keys()) # Calculate the chunk size (roughly equal parts) total_keys = len(keys) chunk_size = total_keys // num_chunks remainder = total_keys % num_chunks # Split the data into chunks and save each chunk to a separate file start = 0 for i in range(num_chunks): # Determine the end index for each chunk end = start + chunk_size + (1 if i < remainder else 0) # Create the chunked dictionary chunk_keys = keys[start:end] chunk_data = {key: data[key] for key in chunk_keys} # Generate a file name for the chunk output_file = f"{output_prefix}_{i + 1}.json" # Write the chunk data to a new JSON file with open(output_file, 'w') as chunk_file: json.dump(chunk_data, chunk_file, indent=4) print(f"Saved chunk {i + 1} to {output_file}") start = end # Example usage split_json('full_gavl_polygons_obj.json', 'smaller_jsons/output_chunk', 20)