JSON and the Blarghonauts, or, Firefox and Pretty-Printing FAIL

Posted by Michael Giarlo on December 02, 2008

There must be a better way of viewing pretty-printed JSON from Firefox than this. (EDIT: Hail, JSONovich!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# ~/bin/jsonhandler.py 
# Take some JSON from a file or stdin, format it, output to a tempfile, 
# open in EDITOR
 
from __future__ import with_statement
import os
import sys
import simplejson
import tempfile
 
EDITOR = "/usr/bin/gedit"
 
if __name__ == "__main__":
    if len(sys.argv) == 2:
        # if invoked as jsonhandler.py {FILE}
        json = open(sys.argv[1])
    else:
        # if JSON is piped in (e.g., from Firefox, 
        # or cat {FILE} | jsonhandler.py)
        json = sys.stdin
    json = simplejson.load(json)
    # the with_statement is kind of gratuitous but I like it
    with open(tempfile.mktemp('.json'), 'w') as jsonfile:
        simplejson.dump(json, jsonfile, indent=4)
    # all of that and gedit doesn't even highlight JSON
    # I have emacs highlighting JSON but this generates a "stdin is not 
    # a tty" error, so EDITOR is not set to emacs
    # xemacs works a little better, but I need to click:
    # "Options > Syntax Highlighting > In this buffer" every time, despite 
    # saving to custom.el, so EDITOR is not set to xemacs
    # Very annoying!
    os.system("%s %s" % (EDITOR, jsonfile.name))

And then I set ~/bin/jsonhandler.py as the action for application/json in Edit | Preferences | Applications.

Yuck. Help?

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments