If you work a lot with JSON you probably find yourself having to turn unreadable JSON into a readable format.
I recently wrote a small application that takes the most recent contents in your clipboard and attempts to JSON parse it. If it can, then it re-pastes the formatted JSON back to your clipboard. If it can’t be parsed, it simply goes about merrily.
# -*- coding: utf-8 -*- import pyperclip import simplejson as json import time def format_clipboard_to_json(): recent_value = "" while True: cb_content = pyperclip.paste() if cb_content != recent_value: try: parsed = json.loads(cb_content) # must pass `sort_keys=True` because json isn't guaranteed to parse # and return the same sort order each time pretty_content = json.dumps(parsed, sort_keys=True, indent=2) pyperclip.copy(pretty_content) recent_value = pretty_content except ValueError: pass time.sleep(1) if __name__ == '__main__': format_clipboard_to_json()
When invoked via the start.sh command from the repo, the script will run in the background and will not terminate if logged out of the shell – see nohup.
If you wish to kill the process, you can run the below commands to find and kill this process.
$ lsof -c Python $ kill -9 <PID>
Check out the source code on my github profile