JSON to CSV Ruby Script

Posted July 26, 2010

Spent a good 5 minutes trying to dig up a small JSON to CSV ruby script on google but couldn't find anything! So i wrote my own small script in Ruby. Hope this saves someone 5 minutes!

FORMAT

[{"first":"Richard", "last":"Ramsden"}, {"first":"Steven", "last":"Ramsden"}]

 

JSON to CSV

require 'rubygems'
require 'json'
require 'csv'

str = File.open("data.txt", "r").read()

out = File.open("sample.csv", "w")
json = JSON.parse(str)

# figure out key values
keys = []
json[0].each { |k,v| keys.push(k) }

# write header
(0..keys.size).each do |i|
  out.write(keys[i])
  out.write(",") if i < keys.size-1
end

# iterate and write values
json.each { |a|
  out.write("\n")
  a.each { |k,v|
    out.write(v)
    out.write(",") if k != keys.last
  }
}

 

CSV to JSON

require 'rubygems'
require 'json'

file = File.open("sample.csv", "r")

# get header
keys = file.readline().chomp!.split(",")
values = []
result = []

# parse values
file.each_line do | line |
  iter = 0
  assoc = {}
  line.chomp.split(",").each do |v|
    assoc[keys[iter]] = v
    iter += 1
  end
  result.push(assoc)
end

puts result.to_json

Vancouver Island Spine Trail

Posted April 14, 2010

I attended my first monthly meetup for Outdoor Vanisle this month at the Longwood Brewpub in Nanaimo. I was fortunate to catch a presentation on the proposed VI Spine trail setup by Gil Parker a member of the Vancouver Island Spine Association (VISTA). What is the VI Spine? It's a proposed 700km hiking trail that will run the length of Vancouver Island, connecting communities and wilderness. VISTA is planning to raise awareness of the trail by putting on a relay starting up in June. It will include hiking, biking, and running from Cape Scott to Victoria!

If you're looking for more information on the trail check out VISTA's website http://www.vispine.ca/

Leg Date Location Mode of Transportation
1 June 5 Cape Scott Parking Lot to Nissen Bight Hike
2 June 6 North Coast Trail to Shushartie Bay Run
3 June 7 Shushartie River to Port Hardy Mountain Bike
  June 8 COMMUNITY DAY in Port Hardy  
 4  June 9-10  Port Hardy to Campbell River  Road Bike
   June 9  COMMUNITY Evening in Woss  
   June 11  COMMUNITY DAY in Campbell River  
 5  June 11  Campbell River to Mt. Washington  Road Bike
   June 11  COMMUNITY Evening at Mt. Washington  
 6  June 12-14  Mt. Washington to Cumberland  Hike and snowshoe
   June 15  COMMUNITY DAY in Cumberland  
 7  June 16  Cumberland to Mt. Clifton  Mountain Bike
 8  June 17-19  Mt. Clifton to Port Alberni  Hike and snowshoe
   June 20  COMMUNITY DAY in Port Alberni  
 9  June 21-23  Port Alberni to Lake Cowichan  Mountain Bike
   June 24  COMMUNITY DAY in Lake Cowichan  
 10  June 25-26  Lake Cowichan to Victoria  Run and Horseback
   June 27  COMMUNITY DAY in Victoria  

View older posts »