import os
import json

def get_directory_tree(directory):
    tree = {}
    
    # Walk through the directory recursively
    for root, dirs, files in os.walk(directory):
        # Get the path of the current folder relative to the root
        folder_path = os.path.relpath(root, directory).split(os.sep)
        
        # Start at the root level of the tree
        subdir = tree
        
        # Traverse to the current folder level in the tree
        for folder in folder_path:
            if folder == '.':  # Skip the current directory symbol
                continue
            subdir = subdir.setdefault(folder, {})
        
        # Add files as leaves in the current folder
        for file in files:
            subdir[file] = None  # Files are leaves with None as their value
    
    return tree

def save_to_json(data, json_file):
    with open(json_file, 'w') as f:
        json.dump(data, f, indent=4)

if __name__ == "__main__":
    directory_to_scan = "REPORTS"  # Change this to your directory path
    json_output_file = "directory_tree.json"  # Output JSON file

    # Get directory tree structure
    directory_tree = get_directory_tree(directory_to_scan)

    # Save to JSON
    save_to_json(directory_tree, json_output_file)

    print(f"JSON file saved as {json_output_file}")

