Skip to content

Instantly share code, notes, and snippets.

@riordant
Created July 10, 2025 11:03
Show Gist options
  • Save riordant/d70e7cd46e41505e8ce6612e63a558bf to your computer and use it in GitHub Desktop.
Save riordant/d70e7cd46e41505e8ce6612e63a558bf to your computer and use it in GitHub Desktop.
Format ChatGPT Chat History (from ZIP download)
# run in extracted folder: python3 format_chatgpt_history.py
import json
from pathlib import Path
def extract_conversations_to_markdown(json_path: Path, output_path: Path):
with open(json_path, "r", encoding="utf-8") as f:
conversations = json.load(f)
def traverse_messages(mapping, node_id):
messages = []
node = mapping.get(node_id)
if not node:
return messages
message = node.get("message")
if message:
author = message.get("author", {}).get("role")
content = message.get("content", {})
parts = content.get("parts")
if parts and isinstance(parts, list) and parts[0].strip():
prefix = "User:" if author == "user" else "System:"
messages.append(f"{prefix} {parts[0].strip()}")
for child_id in node.get("children", []):
messages.extend(traverse_messages(mapping, child_id))
return messages
output_lines = []
for conv in conversations:
title = conv.get("title", "Untitled Conversation")
mapping = conv.get("mapping", {})
if not mapping:
continue
output_lines.append(f"# {title}\n")
messages = traverse_messages(mapping, "client-created-root")
output_lines.extend(messages)
output_lines.append("\n---\n")
output_path.write_text("\n\n".join(output_lines), encoding="utf-8")
print(f"Exported markdown to: {output_path}")
json_file = Path("./conversations.json")
output_file = Path("./conversations.md")
extract_conversations_to_markdown(json_file, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment