Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created September 22, 2015 15:41
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 msonnabaum/131ac2315e578d583afa to your computer and use it in GitHub Desktop.
Save msonnabaum/131ac2315e578d583afa to your computer and use it in GitHub Desktop.
#!/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