Skip to content

Instantly share code, notes, and snippets.

@tlehman
Created June 11, 2019 18:47
Show Gist options
  • Save tlehman/af602be1fc4cc2fe43516c943c3b516e to your computer and use it in GitHub Desktop.
Save tlehman/af602be1fc4cc2fe43516c943c3b516e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# display tree of json key names
# like tree(1) for JSON
# by @tlehman
require 'json'
input = STDIN.read || File.open(ARGV.first).read
parsed_input = JSON.parse(input)
def puts_keys(hash, indent_level = 0)
# and all hash key children
indent = " " * indent_level
hash.keys.each do |key|
puts indent + key
if hash[key].is_a?(Hash)
puts_keys(hash[key], indent_level + 1)
end
end
end
puts_keys(parsed_input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment