From 0bfa1a5d58f6714843229c3965954cd488d52e30 Mon Sep 17 00:00:00 2001 From: Scott Maslar Date: Wed, 10 Aug 2016 21:21:54 -0700 Subject: [PATCH] Update csv to json script I've updated the data_to_json script to support better options and allow for defining a default OKC mapping. --- README.md | 10 +++- sandbox/data_to_json.rb | 112 ++++++++++++++++++++++------------------ 2 files changed, 71 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index efb2b852..d8637d36 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,15 @@ We have a small script written in ruby for generating a json file from a csv of To run the script ``` $ cd /sandbox -$ ruby data_to_json.rb +$ ruby data_to_json.rb -f +``` + +The script supports the following options +``` +Usage: data_to_json.rb [options] + -o, --okc Run with OKC data mappings + -f, --file [file] CSV file to convert + -h, --help Display this screen ``` ### Harp diff --git a/sandbox/data_to_json.rb b/sandbox/data_to_json.rb index d558605c..133adcd3 100644 --- a/sandbox/data_to_json.rb +++ b/sandbox/data_to_json.rb @@ -1,65 +1,77 @@ require 'csv' require 'json' +require 'pry' +require 'optparse' + +DATA_KEYS = %w[agency fund lob program_name account amount] +OKC_MAPPING = { 'agency' => 1, + 'fund' => 1, + 'lob' => 1, + 'program_name' => 1, + 'account' => 1, + 'amount' => 1 } + +options = {} +OptionParser.new do |opts| + options[:okc] = false + options[:file] = '' + + opts.banner = "Usage: data_to_json.rb [options]" + + opts.on("-o", "--okc", "Run with OKC data mappings") do |okc_mapping| + options[:okc] = true + end + + opts.on("-f", "--file [file]", String, "CSV file to convert") do |file_name| + options[:file] = file_name + end + + opts.on( '-h', '--help', 'Display this screen' ) do + puts opts + puts "" + exit + end +end.parse! + +if options[:file].empty? + puts "Please supply the name of the csv file:" + puts "Probably something like 'CODE for OKC FY16 Final.csv'" + options[:file] = gets.strip +end -puts "Please supply the name of the file" - -file_name = gets.strip -csv_file_name = "#{file_name}.csv" - -csv = CSV.read(csv_file_name, headers: true) +csv = CSV.read(options[:file], headers: true) headers = csv.headers -p headers - -puts "Please select the number corresponding to the agency:" -headers.each_with_index { |header, index| puts "#{index}: #{header}"} - -agency = gets - -puts "Please select the number corresponding to the account:" -headers.each_with_index { |header, index| puts "#{index}: #{header}"} - -account = gets - -puts "Please select the number corresponding to the fund:" -headers.each_with_index { |header, index| puts "#{index}: #{header}"} - -fund = gets +mapping = {} -puts "Please select the number corresponding to the operating unit:" -headers.each_with_index { |header, index| puts "#{index}: #{header}"} +if options[:okc] + mapping = OKC_MAPPING +else + DATA_KEYS.each do |key| + print "Please select the number corresponding to the #{key}:\n" + headers.each_with_index { |header, index| puts "#{index}: #{header}"} -operating_unit = gets - -puts "Please select the number corresponding to the operating lob:" -headers.each_with_index { |header, index| puts "#{index}: #{header}"} - -lob = gets - -puts "Please select the number corresponding to the program name:" -headers.each_with_index { |header, index| puts "#{index}: #{header}"} - -program_name = gets - -puts "Please select the number corresponding to the amount:" -headers.each_with_index { |header, index| puts "#{index}: #{header}"} - -amount = gets + mapping[key] = gets.to_i + end +end data = [] -stuff = [] csv.each do |row| data << { - agency: row[headers[agency.to_i]], #row["Account Description"], - #account: row[headers[account.to_i]], #row["Account Description"], - fund: row[headers[fund.to_i]], #row["FundDescription"], - #unit: row[headers[operating_unit.to_i]], #row["OperatingUnitDescription"], - lob: row[headers[lob.to_i]], #row["OperatingUnitDescription"], - program: row[headers[program_name.to_i]], #row["OperatingUnitDescription"], - key: row[headers[account.to_i]], #row["ProgramName"], - value: row[headers[amount.to_i]] #row["Budget"].to_i + agency: row[headers[mapping['agency']]], + fund: row[headers[mapping['fund']]], + lob: row[headers[mapping['lob']]], + program: row[headers[mapping['program_name']]], + key: row[headers[mapping['account']]], + value: row[headers[mapping['amount']]] } end -file = File.new("#{file_name}.json" , 'w') +json_file_name = "#{options[:file].chomp('.csv')}.json" +file = File.new(json_file_name, 'w') + +puts "" +puts "# Writing #{json_file_name}" +puts "" + file.write JSON.pretty_generate(data)