Skip to content

Instantly share code, notes, and snippets.

@pritibiyani
Last active August 29, 2015 14:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pritibiyani/e1f3a3fdd6d6172d93dd to your computer and use it in GitHub Desktop.
[Script] [extract_specific_param_from_json] This Script extracts specific parameters from json.
#! /usr/bin/env ruby
# encoding: UTF-8
require 'date'
require 'json'
# Utility for making extractors.
Extractor = Struct.new(:field, :transformation)
def as_is field
Extractor.new(field, lambda {|value| value})
end
def as_readable_date field
time_format = "%d %b %Y %H:%M:%S:%L"
Extractor.new(field, lambda {|value| Time.at(value / 1000).strftime(time_format) })
end
# Actual extraction from JSON
def extract_all json, extractors
out = {}
extractors.each do |extractor|
field = extractor.field
f = extractor.transformation
out[field] = f.call(json[field]) rescue nil
end
out
end
# Extractors for parameters.
@identifying_parameters = [
as_is("id"),
as_is("masterid"),
as_is("locale")
]
@publish_parameters = [
as_is("versionInLiveSlice"),
as_is("readyForLiveSlice"),
as_is("readyOnLiveSlice"),
as_readable_date("createdAtDate"),
as_readable_date("updatedAtDate"),
as_readable_date(" "),
as_readable_date("sunriseDate"),
as_readable_date("sunsetDate")
]
@all_parameters = @publish_parameters + @identifying_parameters
def extract_all_params json
extract_all json, @all_parameters
end
# Scripty part
# Helpers
def usage
"usage: #{$PROGRAM_NAME} <path_to_json_file> ?<array|value>"
end
def valid_type? type
["array", "value"].member?(type)
end
def read_file_utf8 file_path
File.open(file_path).read.force_encoding("utf-8")
end
# Read input. Fail if bad input.
file_path = nil
type = nil
class Array
def at index
elem = self[index]
raise "Bad index" if elem.nil?
elem
end
end
begin
file_path = ARGV.at(0)
type = ARGV.at(1) rescue "value"
raise "invalid_type #{type}" unless valid_type?(type)
rescue Exception => ex
puts ex
puts usage
exit 1
end
file_contents = read_file_utf8(file_path)
def print_readably hash
hash.each do |key, value|
puts "#{key} => #{value}"
end
end
case type
when "value"
json_data = JSON.parse(file_contents)
out = extract_all_params json_data
print_readably out
when "array"
jsons = file_contents.split(/\n/).map {|line| JSON.parse(line)}
jsons.each do |json|
out = extract_all_params json
print_readably out
puts ''
end
end
@PritiBiy
Copy link

./extract_publish_parameters.rb some_json.json

versionInLiveSlice =>
readyForLiveSlice =>
readyOnLiveSlice =>
createdAtDate =>
updatedAtDate => 25 Mar 2014 02:55:19:000
sunriseDate => 14 Jan 2014 20:49:08:000
sunsetDate=>
id => urn:sony:product:super:product
masterid => urn:sony:product:master_id
locale => es_MX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment