Skip to content

Instantly share code, notes, and snippets.

@alterisian
Created May 8, 2024 14:50
Show Gist options
  • Save alterisian/351fef998c5d3a26de562311a26b8207 to your computer and use it in GitHub Desktop.
Save alterisian/351fef998c5d3a26de562311a26b8207 to your computer and use it in GitHub Desktop.
Got a massive blob of JSON? Got your rails console or irb open? Past in this method, and get a summary of the keys in the JSON!
# usage: json_keys(json_data, mode: 'summary')
def json_keys(data, mode: 'summary', depth: 0)
indent = ' ' * depth # Indentation based on the nesting level
case data
when Array
json_keys(data.first, mode: mode, depth: depth + 1) if data.any?
when Hash
data.each do |key, value|
puts "#{indent}#{key}" if mode == 'summary'
json_keys(value, mode: mode, depth: depth + 1)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment