Turtl to MD directory [python]

Turtl's output JSON is a bit annoying, so here's a basic script to transfer text and password notes (only) to a markdown directory.
It's very limited in features since I only built it for personal use.
Place your output JSON (called turtl-backup.json) in the same folder, and create a folder called turtl.

NB: it ignores boards, users, and creation dates, and only copies notes that are in text or password form

I would upload the python file but discourse doesn't seem to like me doing that, so here's the source code in plain text. Yes it's not as efficient as it can be but it only has to work once.

import json, os
OUTPUT = "turtl/"

if OUTPUT != "" and OUTPUT[-1] != "/":
    OUTPUT += "/"

def createUnnamedFile(board="", num=0):
    try:
        f = open(OUTPUT + board + f"Untitled-{num}.md", "x")
        f.close()
        return "Untitled-%i" % num
    except FileExistsError:
        return createUnnamedFile(board, num + 1)

with open("turtl-backup.json") as f:
    data = json.load(f)

boards = {}
for b in data["boards"]:
    boards[b["id"]] = str(b["title"])
    try:
        os.mkdir(OUTPUT + b["title"])
    except FileExistsError:
        print("board directory already exists")

for n in data["notes"]:
    outdir = ""
    if n["board_id"] != None:
        try:
            outdir = boards[n["board_id"]] + "/"
        except KeyError:
            print("board not found")
            print("board id:", n["board_id"])
    
    if n["title"] == "":
        name = createUnnamedFile(outdir) + ".md"
    else:
        name = n["title"].strip() + ".md"
    
    outData = []
    if n["type"] == "text":
        outData = [n["text"]]
    elif n["type"] == "password":
        outData = [n["password"]]
    #print(len(outData))
    with open(f"{OUTPUT}{outdir}{name}", "w") as f:
        f.writelines(outData)

I doubt many people will have a use for this as a lot of users moved away from Turtl after it kinda stopped being maintained in 2019, but if any future people have a need for this I hope it is helpful. I know someone else on here gave a PHP version, but not a lot of people can easily run PHP so I figured this was still worth posting.
If this has errors or bugs, that's tough luck because I forgot to save the password when I made this account :stuck_out_tongue:

Thanks for posting it, I've added it to Importing notes from other notebook applications

1 Like