Created
May 8, 2024 14:50
-
-
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!
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
# 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