Skip to content

Instantly share code, notes, and snippets.

@nvkv
Created December 14, 2011 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvkv/1475984 to your computer and use it in GitHub Desktop.
Save nvkv/1475984 to your computer and use it in GitHub Desktop.
Very simple and dirty json CLI query tool
#!/usr/bin/env ruby
# Very simple and dirty json CLI query tool
require 'rubygems'
require 'json'
def numeric?(s)
Float(s) rescue false
end
this = JSON[STDIN.read]
js_path = ARGV[0].split('.')
unless ARGV[0] == "-e"
query_string = "#{js_path[0]}"
query_string = query_string + js_path[1..-1].reduce("") do |s, p|
if numeric? p
"#{s}[#{p}]"
else
"#{s}['#{p}']"
end
end
else
query_string = ARGV[1]
end
result = eval(query_string)
begin
puts JSON.pretty_generate(result)
rescue Exception => e
puts result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment