How to Convert Telegram Chat Export JSON into CSV
Source: Dev.to
Problem
Telegram Desktop allows you to export chats, but the export format is JSON.
When you need to work with a Telegram chat outside of Telegram, the JSON export presents several issues:
- Messages can be nested and formatted inconsistently.
- Emojis and multiline messages often break spreadsheets.
- System messages are mixed with real messages.
- Large exports can crash Excel entirely.
In practice, this makes Telegram exports hard to use for anything other than storage.
Solution Overview
The high‑level approach is straightforward:
- Load the exported
result.json. - Iterate through the
messagesarray. - Normalize each message into a flat structure.
- Write everything to a properly quoted CSV file.
The tricky part is handling edge cases such as multiline text, emojis, media messages, and system/service messages.
Minimal Example
import json
import csv
with open("result.json", encoding="utf-8") as f:
data = json.load(f)
with open("chat.csv", "w", newline="", encoding="utf-8") as out:
writer = csv.writer(out)
writer.writerow(["date", "sender", "message"])
for msg in data.get("messages", []):
date = msg.get("date", "")
sender = msg.get("from", "Unknown")
text = msg.get("text", "")
# If the text field is a list, concatenate its parts
if isinstance(text, list):
text = "".join(
t.get("text", "") if isinstance(t, dict) else t
for t in text
)
writer.writerow([date, sender, text])
This demonstrates the core idea. A production‑ready solution would need to handle many more edge cases, such as:
- Properly escaping commas and quotes.
- Preserving emojis and other Unicode characters.
- Extracting and linking media files.
- Filtering or labeling system messages.