Bob Ippolito (@etrepum) on Haskell, Python, Erlang, JavaScript, etc.
«

Python 2.6 released, now with json! :)

»

Python 2.6 was released yesterday, which has tons of cool new features including a new json library and a new multiprocessing library. The json library is basically simplejson (from a few months ago) minus the Python 2.4/2.5 support and refactored to take advantage of the latest future-compatible features (such as the new str.format method instead of using % format interpolation).

The only downside is that I wasn't able to get the latest simplejson 2.0.1 performance enhancements into the first release of Python 2.6, but expect them for Python 2.6.1! Don't worry though, simplejson should install just fine with Python 2.6 if you need the speed. If not, you don't have any dependencies for JSON anymore, just change your imports:

# Use simplejson or Python 2.6 json, prefer simplejson.
try:
    import simplejson as json
except ImportError:
    import json

print repr(json.dumps({'key': 'serialize this!'}))
print repr(json.loads('{"key": "deserialize this!"}'))

One of my favorite features in json/simplejson is the shell command that will validate/pretty print JSON. Great for debugging, and it will be sweet to have it available on every box with Python 2.6+!

A really cool tip if you're using Mac OS X is that you can access the string version of the pasteboard from the shell with pbpaste and pbcopy. This will take the JSON from your pasteboard and replace it with a pretty-printed version! If you're using simplejson just change json.tool to simplejson.tool. I'm sure this would be super handy in a text editor macro too:

$ pbpaste | python -mjson.tool | pbcopy