# 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)