JSON For Structured Command-line Application Integration

| | Comments (1) | TrackBacks (0)

Recently I've been dorking around with Python, using it to manage enhanced ctorrent instances. One of the difficulties is that the stdout from ctorrent and nearly all command-line/console applications are plain, unstructured, text.

Perforce is a clear exception.

With the -G switch, all output will be Python marshalled dictionary objects.

Using the output of p4 -G is as simple as marshal.load( output ).

But of course this is limited to python.

Turns out there are a number of JSON libraries for various languages.

If more command line applications provided structured output, and could handle structured input, we could script together much more interesting apps in bash, python, ruby, or whatever. All without cryptic and brittle regex invocations.

And yes, sed, grep, awk, etc are for creating structure where there is none, or using that structure. Perfect for things like this:

strings 'some.torrent' | sed -n 's/.*name[0-9]*:\([^:]*\..\{3\}\)[0-9]*:.*/\\1/p'

assuming you want to get the filename hidden inside a torrent. But very brittle as this will likely break in the presence of a multi-file torrent.

Anyways, I don't think JSON has to be just for web applications.

0 TrackBacks

Listed below are links to blogs that reference this entry: JSON For Structured Command-line Application Integration.

TrackBack URL for this entry: http://www.manamplified.org/cgi-bin/mt-tb.cgi/341

1 Comments

Here, here. If I had a nickle for every time I wished the Universal Feed Parser had JSON output, well I could probably buy myself a nice steak.

Leave a comment