4 Replies - 1320 Views - Last Post: 17 September 2013 - 03:18 PM Rate Topic: -----

#1 dknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-July 13

A ruby script to convert all csv files in a folder to json

Posted 16 September 2013 - 06:21 PM

I am trying to build a script to convert all .csv files in a folder to .json. I have a script with me where I use FasterCSV to convert individual CSVs to JSON i.e like this:

#!/usr/local/bin/ruby
require "rubygems"
require "fastercsv"
require "json"

jsonFile = File.open("some.json",'w');
jsonData = {}

FasterCSV.foreach("some.csv",
  :headers => true, :header_converters => :symbol) do |row| 

   # puts "Row is #{row}"
  jsonData[somename] = {
    :name => row[2],
    :length => row[3],
    :lat => row[4],
    :long => row[5],
  }; 
  # puts "data is #{jsonData[jsonData.length-1]}"   
    
  jsonFile.write(JSON.pretty_generate(jsonData));



But how can I change this to search all csv files in a folder and generate corresponding JSON.

2. One other thing I am stuck with is how to generate nested JSON i.e

currently my JSON is like this:

{
  "NAME1": {
    "LAT": "37.847048",
    "Name": "SDFSDG",
    "length": "0.03",
    "LON": "-123.3433"
  }
}{
  "NAME1": {
    "LAT": "37.847048",
    "Name": "SDFSDFSDAFDG",
    "length": "0.03",
    "LON": "-123.32334433"
  }
}



One issue with above is that NAME1 is redundant. So I want to add it to the top and then have 2 seperate fields with other data like "lat", "lon", "length".

I would really appreciate the help.

Is This A Good Question/Topic? 0
  • +

Replies To: A ruby script to convert all csv files in a folder to json

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,515
  • Joined: 28-November 09

Re: A ruby script to convert all csv files in a folder to json

Posted 16 September 2013 - 07:45 PM

http://ruby-doc.org/...l#method-c-glob

Directory Glob is your friend for finding all files of one type. Then all you have to do is use an each on that list of files and use their names as the header.

Ah, and take the advice on the Ruby mailing list to your email, that first reply is a basic directory glob.

This post has been edited by Lemur: 16 September 2013 - 07:58 PM

Was This Post Helpful? 1
  • +
  • -

#3 dknight  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 31-July 13

Re: A ruby script to convert all csv files in a folder to json

Posted 16 September 2013 - 09:33 PM

View PostLemur, on 16 September 2013 - 07:45 PM, said:

http://ruby-doc.org/...l#method-c-glob

Directory Glob is your friend for finding all files of one type. Then all you have to do is use an each on that list of files and use their names as the header.

Ah, and take the advice on the Ruby mailing list to your email, that first reply is a basic directory glob.


Thanks
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: A ruby script to convert all csv files in a folder to json

Posted 17 September 2013 - 03:33 AM

View PostLemur, on 17 September 2013 - 04:45 AM, said:

use an each on that list of files


glob takes a block, so you don't need an each. That way you save 5 characters (.each) and the creation of an array.

This post has been edited by sepp2k: 17 September 2013 - 03:33 AM

Was This Post Helpful? 1
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,515
  • Joined: 28-November 09

Re: A ruby script to convert all csv files in a folder to json

Posted 17 September 2013 - 03:18 PM

View Postsepp2k, on 17 September 2013 - 05:33 AM, said:

View PostLemur, on 17 September 2013 - 04:45 AM, said:

use an each on that list of files


glob takes a block, so you don't need an each. That way you save 5 characters (.each) and the creation of an array.


One of these days I'll catch more things like that. Thank ya kindly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1