Created
September 22, 2015 15:41
-
-
Save msonnabaum/131ac2315e578d583afa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "json" | |
def flatten_hash(hash, parent_key = "") | |
result_hash = {} | |
hash.each do |key, value| | |
if value.is_a? Hash | |
result_hash.merge! flatten_hash(value, key) | |
else | |
result_key = if parent_key.empty? then key else "#{parent_key}.#{key}" end | |
result_hash[result_key] = value | |
end | |
end | |
result_hash | |
end | |
file = STDIN.read | |
hash = JSON.parse file | |
flat_hash = flatten_hash hash | |
puts JSON.pretty_generate(flat_hash) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment