This documentation provides a comprehensive guide for converting exported ChatGPT conversation data into Markdown files for use with i.e. Obsidian.
This conversion process facilitates easy access and management of conversations within note-taking applications like Obsidian, allowing you to leverage the organizational and linking capabilities of such tools for your ChatGPT interactions.
A Detailed Guide For Beginners
This step-by-step guide is designed for beginners to convert their exported ChatGPT conversation data into Markdown files.
These files can be easily accessed and managed within Obsidian, a powerful note-taking application that supports Markdown.
Before You Start
- Python Installation: Make sure Python (version 3.6 or newer) is installed on your computer. If you haven't installed it yet, download it from python.org and follow the installation instructions. Remember to check the option to Add Python to PATH during installation.
- Export Your ChatGPT Data: Export your conversation data from ChatGPT in JSON format. This usually involves requesting your data from the ChatGPT platform and receiving a ZIP file containing your conversations in JSON format.
- Obsidian Installation: If you plan to use Obsidian to view and manage your Markdown files, ensure it's installed on your computer. Download it from Obsidian's official site.
You should now have fulfilled all the
Prerequisites
- Python: Ensure Python 3.6 or newer is installed on your system.
- Data File: Your ChatGPT conversation data should be exported in JSON format.
- Obsidian: For viewing and managing the generated Markdown files, Obsidian (or any Markdown-compatible note-taking application) should be installed.
Overview
The provided Python script automates the conversion of ChatGPT conversation data, stored in JSON format, into individual Markdown (.md) files. Each conversation is turned into a Markdown file with a structured frontmatter section, making it compatible with Obsidian and similar Markdown-based note-taking applications.
1. Preparing Your JSON Data
1.1 Extract the ZIP File
Locate the ZIP file containing your exported ChatGPT data.
Right-click on the file and select "Extract All…" or use a similar option depending on your operating system.
Choose a destination folder where you want to extract the files and proceed.
1.2 Find the JSON File
Open the folder where you extracted your files.
Look for a file named conversations.json
or similarly, which contains your conversation data.
1.3 Make a Copy of Your JSON File
To preserve the original data, make a copy of the JSON file.
Right-click on the file, select "Copy," then right-click in an empty area within the folder and select "Paste."
Rename this copy to something easily recognizable, like conversations-copy.json
.
2. Setting Up the Python Script
2.1 Download the Script
Obtain the Python script provided for converting JSON data to Markdown.
Save this script in the same folder as your JSON data for convenience.
To do that
- open something like Studio Visual Code
- copy the version of the script taht suits you
- and save it as
script_name.py
where script_name is the name of your script like chatgpt_json-to-markdown.py
Default Version overwrite existing Markdown files
```pythonimport jsonimport reimport osfrom datetime import datetime
def sanitize_filename(title): """Sanitize the title to create a valid filename.""" sanitized_title = re.sub('[^a-zA-Z0-9 \\\\n.]', '', title).replace(' ', '_') return sanitized_title
def write_message_to_file(file, message): """Format and write a message to the markdown file, ensuring content starts on a new line.""" author = "User" if message["author"]["role"] == "user" else "Assistant" content = message["content"]["parts"][0] # Assuming single part content for simplicity file.write(f"{author}: \\\\n\\\\n{content}\\\\n\\\\n")
def format_datetime(timestamp): """Convert timestamp to readable format.""" if timestamp: return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S') return "N/A"
def create_markdown_file(conversation, output_dir): """Create a markdown file from a conversation dictionary, including frontmatter.""" title = conversation['title'] filename = sanitize_filename(title) + ".md" filepath = os.path.join(output_dir, filename)
with open(filepath, 'w', encoding='utf-8') as file:
# Write frontmatter
file.write(f"