Skip to content

Instantly share code, notes, and snippets.

@weaksauce
Forked from gschanuel/code2.rb
Last active April 27, 2022 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save weaksauce/69b580d087887f729f66de1c4bb0d193 to your computer and use it in GitHub Desktop.
Save weaksauce/69b580d087887f729f66de1c4bb0d193 to your computer and use it in GitHub Desktop.
require 'json'
file = File.read('./event.json')
event = JSON.parse(file)
keys = event['windows']['perfmon']['metrics']
hash=keys.map { |key, value|
key.split('_').reverse.reduce(value) {
|key_value, next_key| {
next_key => key_value
}
}
}
class Hash
def deep_merge(other_hash, &block)
dup.deep_merge!(other_hash, &block)
end
# Same as +deep_merge+, but modifies +self+.
def deep_merge!(other_hash, &block)
merge!(other_hash) do |key, this_val, other_val|
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
this_val.deep_merge(other_val, &block)
elsif block_given?
block.call(key, this_val, other_val)
else
other_val
end
end
end
end
val = {}
hash.each do |x|
val.deep_merge!(x)
end
puts val.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment