Prettify JSON in your clipboard using Python

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>

Cheers!

Check out the source code on my github profile

“I don’t know”…and that’s ok

I have been professionally developing software for almost two years now. In those two years, I have been exposed to a cornucopia of technologies, languages, and paradigms — from the network level all the way up to making nice little animations on page load. In this short time, I have learned that the most valuable tool I have in my toolbox are these three words — “I don’t know” (although debugging an ajax request is a close second). It has been an arduous process to come to this conclusion and one that has taught me more about myself and my skills than I could have foreseen. Why do I say this is my most valuable tool?

Let me outline a couple of key concepts that I hope will help you understand my point.

  1. The software industry is continually evolving, developing and turning over on itself When I first entered the industry, I wanted to know everything I could about the latest and greatest. I am confident that if a framework had a todo list tutorial, that I had done it. Hacker News opened in every new Chrome tab; I had too many bookmarks to count on topics that I told myself “I’d get around to one day”, and I was hungry for knowledge. All of this whilst not actually understanding much of what I was doing. In an ever-changing industry, it is easy to become overwhelmed with the current software darling. However, one thing I know to be true is that the basics will always be the foundation. If you are stumped on newshinything.js remember that at the end of the day it is JavaScript — start there! Eventually, your knowledge of the basics will ultimately make it easier and more satisfying to read, write, and debug in your day-to-day.
  2. Learn to detach yourself from your work This was a hard one for me to understand. Early on in my career I found myself holding on tightly to the work I had done. Afterall, it was a labor of love. I would try and re-try more times than I could count. I read up on architectural patterns and language constructs so that I could craft a well-thought out solution to the problem at hand. Things were going well — until someone decided to offer their opinion. See, I would take it personally; as if a critique on my work was a critique on myself. My solution? — I believed that my co-workers had good intentions. Once I understood that criticism was applied simply because my co-workers wanted to see me perform at a high level, it was then that I could freely expose my work and become better. What a relief to know that others are on your side!
  3. There will (more times than not) be someone else smarter than you in the room This has always been a fun one! After I learned #2, this became a key component of why I love my job. More experience and knowledge in the room means that I have the opportunity to “fast-track” (please don’t read this as “cheat the system” — you still have to put in your own due dilligence). Use other people’s experience to help you build skill in your career. More often than not, senior-level engineers are willing to talk you through a problem and offer advice so long as they see that you are putting in the effort to learn and grow from their wisdom.
  4. Keep failing Failing, for me, accompanied a cyclical thought process
    1. Crisis of confidence — “Why can’t I do this? I mean, I did something way harder last week”
    2. Crisis of career — “Am I even good at this whole programming thing? Maybe I shoud try a different field”
    3. Breakthrough

    If you find yourself feeling this way I ensure you that 1) other people go through the same things and 2) keep failing. With perseverance and the right support, you will be able to tackle the task at hand. And each task accomplished is just another finished problem that you can always look back on and say “I did that!”

  5. I have what it takes I kept reminding myself of this over and over again. When a task seemed overwhelming (as most of them always do), I would remind myself that “I have what it takes.” Remember that each task finished beforehand is just another notch in your confidence belt. Utilize all the strategies and snippets that helped you complete everything before — you have what it takes.