Yay for trivial wrappers!

I have a few simplification scripts here at work to grab some data from various sources and occasionally do something with it. As I was contemplating the best way to add some filtering ability to them it occurred to me that there's no reason at all to embed filtering ability in every single one when the UNIX way was avialable. So I thought and I thought and I procrastinated and eventually I found that someone had done the work for me (I love you internet): JsonPath. Export data in json, run it through this filter for simple stuff (or a proper python/ruby/whatever script for more complicated tasks) and have your action tool accept json as its import. Only problem is that it's in javascript and PHP and neither of those are really ideal for the command line. Enter Python-spidermonkey. Rough around the edges but it does what I need it to: :: #!/usr/bin/env python2.6 import sys import cjson from spidermonkey import Runtime import re rt = Runtime() cx = rt.new_context() cx.eval_script('r=[];') #print "Loading jsonpath..." with file('jsonpath.js') as f: cx.eval_script(f.read()) #print "Parsing json from stdin..." cx.eval_script("i=%s;" %sys.stdin.read()) # for (j in i){ n=i[j]; r.push(%s); }" % (sys.stdin.read(), sys.argv[1])) r = cx.eval_script("jsonPath(i, '%s');" % sys.argv[1].replace('\'', '"')) for line in r: print line #Should probably export json here but this works too Using it is easy: ::$ cat example.json | python pyjsonpath.py "$.store..price" 8.95 12.99 8.99 22.99 19.95 It's up at github if you'd like to extend it or add some error checking.