Skip to content

Instantly share code, notes, and snippets.

@stayingqold
Last active December 16, 2023 23:12
Show Gist options
  • Save stayingqold/25d05006e2251b9608e80637db2c3a59 to your computer and use it in GitHub Desktop.
Save stayingqold/25d05006e2251b9608e80637db2c3a59 to your computer and use it in GitHub Desktop.
Convert Roam titles to he Obsidian Compatible

This script goes through a .json exported roam graph and converts the '/' characters in page titles with a '-' character.

Useful for Obsidian migration, because Obsidian interprets a file with the title '12/16 Math Notes' as a file called '16 Math Notes' in the folder '12/'.

import json

def replace_slashes(data):
    if isinstance(data, dict):
        for key, value in data.items():
            if key == 'title' and isinstance(value, str):
                data[key] = value.replace('/', '-')
            else:
                replace_slashes(value)
    elif isinstance(data, list):
        for item in data:
            replace_slashes(item)

def process_json_file(file_path):
    with open(file_path, 'r') as file:
        data = json.load(file)
    
    replace_slashes(data)

    with open(file_path, 'w') as file:
        json.dump(data, file, indent=4)

# Replace 'your_file.json' with the path to your JSON file
process_json_file('your_file.json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment