Jazor (rhymes with “razor”) is a simple command line JSON parsing tool.
gem install jazor
jazor [options] [source] [expression ...]
See the –help command line option.
The source argument refers to a file, URL or string containing a JSON object. Since attempts to implement a full-featured HTTP client within Jazor would have been futile, Jazor also accepts input from STDIN. This means if you ever need to use an advanced HTTP option that Jazor doesn’t implement, you can always use a real HTTP client (e.g. cURL) and simply pipe the output to Jazor.
Jazor accepts one or more Ruby expressions which are simply eval’ed within the context of your JSON object. After Jazor parses your JSON input into native Ruby data types (Hash, Array, etc.), these expressions are used to slice and dice the data any way you want. The results will be “pretty printed” to STDOUT.
Note that hash keys can be accessed via standard Ruby (e.g. foo, foo.fetch(‘bar’), etc.) or Javascript (e.g. foo.bar) syntax.
Expression testing (–test) allows you to test the “truthiness” of your expression results. If any expression returns a “falsy” value, Jazor will exit with a non-zero return code. This is useful for calling Jazor from within shell scripts.
$ jazor http://github.com/api/v2/json/commits/list/mconigliaro/jazor/master commits.count 16 $ curl --silent http://github.com/api/v2/json/commits/list/mconigliaro/jazor/master | jazor commits.last.message initial commit $ jazor '{ "foo": "abc", "bar": [1, 2, 3] }' 'foo.split(//)' ["a", "b", "c"] $ jazor '{ "foo": "abc", "bar": [1, 2, 3] }' 'bar.inject { |memo,obj| memo + obj }' 6 $ jazor '[1, 2, 3, 4, 5]' 'select { |obj| obj.even? }' [2, 4] $ jazor '[1, 2, 3, 4, 5]' 'select { |obj| obj.odd? }' [1, 3, 5] $ jazor '["a", "b", "c"]' self.count 3
Michael T. Conigliaro <mike [at] conigliaro [dot] org>