Last Updated: February 25, 2016
·
1.367K
· spiegela

Sometimes, I have JSON arrays that I want to see as CSV

Here's my json_to_csv.rb script. Not very sophisticated, but it does the job.

#!/usr/bin/env ruby

require 'rubygems'
require 'json'
raise Exception, 'you must provide a json file' unless ARGV[0]

json = JSON.parse(File.open(ARGV[0]).read)
puts json.first.collect {|k,v| k}.join(',')
puts json.collect {|node| "#{node.collect{|k,v| v}.join(',')}\n"}.join