Skip to content

Instantly share code, notes, and snippets.

@wwalker
Created August 16, 2023 17:51
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 wwalker/ab353396c3ec15f3bdc07bd810186893 to your computer and use it in GitHub Desktop.
Save wwalker/ab353396c3ec15f3bdc07bd810186893 to your computer and use it in GitHub Desktop.
traverse JSON::Any Object
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