Skip to content

Instantly share code, notes, and snippets.

@GwynethLlewelyn
Created December 15, 2022 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GwynethLlewelyn/207b603bf54d3ea0a91cced999a8ca1f to your computer and use it in GitHub Desktop.
Save GwynethLlewelyn/207b603bf54d3ea0a91cced999a8ca1f to your computer and use it in GitHub Desktop.
Convert Twitter JSON direct messages to an easier format

Suppose you need to do a copy & paste of a conversation you had on Twitter to send it by email. Unfortunately, it's very hard to copy & paste it in toto from either the app or the web. Twitter makes it as hard as possible to do so, especially if you wish the whole conversation.

Conversations are conveniently stored as JSON files, but... you need to get access to them. One possible way to do so is the following:

  1. Ask Twitter for a link to an archive of all your data to be downloaded (they'll take up to 24 hours to provide you with it)
  2. Make sure jq is installed
  3. "862741-8538292" represents a conversation between two Twitter IDs (recipientId)
  4. You may have to massage the first line of direct-messages.js so that it reads
{
  "window.YTD.direct_messages.part0" : [
  ...
]}

instead of

window.YTD.direct_messages.part0 = [
...
]

Don't forget to add the extra } at the end!
5. Then run on the shell something like:

$ unzip twitter-YYYY-MMM-lots-of-characters.zip
$ cat twitter-YYYY-MMM-lots-of-characters/data/direct-messages.js | jq -r '."window.YTD.direct_messages.part0"[].dmConversation|select(.conversationId |contains("862741-8538292"))|.messages[].messageCreate|.recipientId + " (" + (.createdAt) + ") " + .text' > output.txt
  1. Remember: everything will come out of order (last messages first)!
  2. jq conveniently does the necessary conversion of embedded \n into newlines; no need to worry about those.
  3. Twitter uses ISO 8601 datetime strings, but with milliseconds, which sadly the macOS version doesn't like
  4. You may optionally use sed to replace the Twitter ID by the user's name (if you know it), etc. etc. etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment