#!/usr/local/env ruby require 'jsonpath' require 'yajl' def usage puts "jsonpath [expression] (file|string) If you omit the second argument, it will read stdin, assuming one valid JSON object per line. Expression must be a valid jsonpath expression." exit! end usage unless ARGV[0] jsonpath = JsonPath.new(ARGV[0]) case ARGV[1] when nil #stdin until STDIN.eof? puts Yajl::Encoder.encode(jsonpath.on(Yajl::Parser.parse(STDIN.readline))) end when String puts Yajl::Encoder.encode(jsonpath.on(Yajl::Parser.parse(File.exist?(ARGV[1]) ? File.read(ARGV[1]) : ARGV[1]))) end