Created
August 16, 2023 17:51
-
-
Save wwalker/ab353396c3ec15f3bdc07bd810186893 to your computer and use it in GitHub Desktop.
traverse JSON::Any Object
This file contains hidden or 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
def walk(prefix : Array(String)) | |
@item = @j if prefix.size == 0 | |
if @item.nil? | |
puts "walk: #{prefix.join(".")} is Nil THIS SHOULD NOT HAPPEN" | |
return | |
end | |
case @item.class | |
when Hash | |
@item.as_h.keys.each do |k| | |
puts "walk: #{prefix + [k]} ia a Hash" | |
walk(prefix + [k]) | |
end # each | |
when Array | |
puts "walk: #{prefix.join(".")} is an Array" | |
puts "[" | |
@item.as_a.each_with_index do |v, i| | |
walk(prefix + [i.to_s]) | |
end # each_with_index | |
puts "]" | |
else | |
puts "walk: #{prefix.join(".")} is a #{@item.class}" | |
end # case | |
end # def walk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment