import json import csv from pathlib import Path # --- CONFIGURATION --- # Replace this with the path to your folder. # The script will read JSONs from here AND save the CSV here. BASE_PATH = 'C:/temp/discord/' OUTPUT_FILENAME = 'C:/temp/output_timestamps.csv' DELIM = ';' # Delimiter for CSV, change if needed def json_to_csv(): # Create a Path object from the global variable work_dir = Path(BASE_PATH) output_path = OUTPUT_FILENAME # Open the CSV file for writing with open(output_path, 'w', newline='', encoding='utf-8') as csv_out: writer = csv.writer(csv_out,delimiter=DELIM) # Write the header row writer.writerow(['Timestamp', 'Filename']) print(f"Scanning: {work_dir}") print(f"Output will be saved to: {output_path}") # Walk through all JSON files for file_path in work_dir.glob('**/*.json'): try: with open(file_path, 'r', encoding='utf-8') as f: data = json.load(f) # Ensure the structure is a list if isinstance(data, list): for entry in data: timestamp = entry.get('Timestamp') # Write row only if timestamp exists if timestamp: writer.writerow([timestamp, file_path.name]) except json.JSONDecodeError: print(f"Warning: Could not decode {file_path.name}") except Exception as e: print(f"Error processing {file_path.name}: {e}") print("Done!") if __name__ == "__main__": json_to_csv()