Sha256: f6237eee6b3b50188963abaf8edc858134ece3e6a75abf1c4a2b77fd5fe9c8db

Contents?: true

Size: 932 Bytes

Versions: 26

Compression:

Stored size: 932 Bytes

Contents

#!/usr/bin/env ruby


# Reads json strings (one per line) from stdin.
# you pass keys as arguments and those are printed out as columns.

require 'rubygems'
require 'json'

$stdout.sync = true

STDIN.each_line do |line|
  begin
    next unless line.include?('}')
    object = JSON.parse(line)
    string = []
    ARGV.each do |argument|
      if object[argument]
        if object[argument].is_a?(String)
          string.push(object[argument])
        elsif object[argument].is_a?(Array)
          object[argument].each do |element|
            string.push("\n" + element.to_json) # will break if not an object!
          end
        else
          string.push(object[argument].to_json)
        end
      else
        string.push(argument)
      end
    end
    if string.size > 0
      puts string.join(" ")
    end
  rescue => error
  # $stderr.puts line
  # $stderr.puts error.message
  # $stderr.puts error.backtrace
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
ix-cli-0.0.6 bin/ix-json-key
ix-cli-0.0.5 bin/ix-json-key
ix-cli-0.0.4 bin/ix-json-key
ix-cli-0.0.3 bin/ix-json-key
ix-cli-0.0.2 bin/ix-json-key
ix-cli-0.0.1 bin/ix-json-key