Here is my Java program and I hope someone finds it useful.
The format should be like this:
"SYMBOL, DATE, CLOSE_PRICE, OPEN_PRICE, HIGH_PRICE, LOW_PRICE, TOM, ADJ_CLOSE
AAIT, 2015-02-26 00: 00: 00.000, -35.152,0,35.152,35.12,679,0
AAL, 2015-02-26 00: 00: 00.000,49.35,50.38,50.38,49.02,7572135.0 "
The first row is the column headings. No quotes anywhere. Separate with commas, not a semicolon. You get a deal.
/* Summary: Converts a CSV file to a JSON file.*/ //import java.util.*; import java.io.*; import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter; public class CSVtoJSON extends JFrame{ private static final long serialVersionUID = 1L; private static File CSVFile; private static BufferedReader read; private static BufferedWriter write; public CSVtoJSON(){ FileNameExtensionFilter filter = new FileNameExtensionFilter("comma separated values", "csv"); JFileChooser choice = new JFileChooser(); choice.setFileFilter(filter); //limit the files displayed int option = choice.showOpenDialog(this); if (option == JFileChooser.APPROVE_OPTION) { CSVFile = choice.getSelectedFile(); } else{ JOptionPane.showMessageDialog(this, "Did not select file. Program will exit.", "System Dialog", JOptionPane.PLAIN_MESSAGE); System.exit(1); } } public static void main(String args[]){ CSVtoJSON parse = new CSVtoJSON(); parse.convert(); System.exit(0); } private void convert(){ /*Converts a .csv file to .json. Assumes first line is header with columns*/ try { read = new BufferedReader(new FileReader(CSVFile)); String outputName = CSVFile.toString().substring(0, CSVFile.toString().lastIndexOf(".")) + ".json"; write = new BufferedWriter(new FileWriter(new File(outputName))); String line; String columns[]; //contains column names int num_cols; String tokens[]; int progress = 0; //check progress //initialize columns line = read.readLine(); columns = line.split(","); num_cols = columns.length; write.write("["); //begin file as array line = read.readLine(); while(true) { tokens = line.split(","); if (tokens.length == num_cols){ //if number columns equal to number entries write.write("{"); for (int k = 0; k < num_cols; ++k){ //for each column if (tokens[k].matches("^-?[0-9]*\\.?[0-9]*$")){ //if a number write.write("\"" + columns[k] + "\": " + tokens[k]); if (k < num_cols - 1) write.write(", "); } else { //if a string write.write("\"" + columns[k] + "\": \"" + tokens[k] + "\""); if (k < num_cols - 1) write.write(", "); } } ++progress; //progress update if (progress % 10000 == 0) System.out.println(progress); //print progress if((line = read.readLine()) != null){//if not last line write.write("},"); write.newLine(); } else{ write.write("}]");//if last line write.newLine(); break; } } else{ //line = read.readLine(); //read next line if wish to continue parsing despite error JOptionPane.showMessageDialog(this, "ERROR: Formatting error line " + (progress + 2) + ". Failed to parse.", "System Dialog", JOptionPane.PLAIN_MESSAGE); System.exit(-1); //error message } } JOptionPane.showMessageDialog(this, "File converted successfully to " + outputName, "System Dialog", JOptionPane.PLAIN_MESSAGE); write.close(); read.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Swing is required, but comes with a small GUI, so those who have absolutely no knowledge of Java can use it after loading it into the .jar executable. Feel free to improve it. Thanks to StackOverflow for helping me all these years.