Skip to content

Instantly share code, notes, and snippets.

@hangingman
Last active April 16, 2019 14:17
Show Gist options
  • Save hangingman/a0c7e966e3fd7cf5184881aab2345c98 to your computer and use it in GitHub Desktop.
Save hangingman/a0c7e966e3fd7cf5184881aab2345c98 to your computer and use it in GitHub Desktop.
JSONを再帰的にたどってkey, valueをCSV化する試み
require "json"
def rec(json, prefix)
if json.class == Hash
json.each do |key, value|
rec(value, "#{prefix},#{key}")
end
elsif json.class == Array
json.each do |item|
rec(item, prefix)
end
else
puts "#{prefix},#{json}"
end
end
File.open("sample.json") do |j|
hash = JSON.load(j)
rec(hash, "")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment