Skip to content

Instantly share code, notes, and snippets.

@mikiobraun
Created June 14, 2010 13:32
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 mikiobraun/437677 to your computer and use it in GitHub Desktop.
Save mikiobraun/437677 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- ruby -*-
# Usage:
# ruby printjson.rb field1 fields2 file
#
# if file == '-' then read from stdin
#
# Install the json gem first:
#
# gem install json
#
# Build your own console twitter-wall with
#
# curl -s http://stream.twitter.com/1/statuses/filter.json?track=<terms-to-track> -u<username>:<password> | ruby printjson.rb created_at text -
require 'rubygems'
require 'json'
fields = ARGV[0...-1]
file = ARGV[-1]
def print_field(json, fields)
fields = fields.split('.')
for f in fields
json = json[f]
end
return json
end
def print_json(line, fields)
begin
json = JSON.parse(line)
puts fields.map {|field| print_field(json, field)}.join "\t"
rescue JSON::ParserError
end
end
if file == '-'
$stdin.each_line do |line| print_json(line, fields) end
else
open(file).each_line do |line| print_json(line, fields) end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment