Converting CSV seed data into a javascript array for use in a script

Document created by Dave Murphy Employee on Dec 15, 2017Last modified by Dave Murphy Employee on Dec 15, 2017
Version 2Show Document
  • View in full screen mode


The attached assets will convert a customer-supplied CSV file into a javascript array format that can be used in a script for seed data randomization. I wrote this using perl. It basically reads the CSV file and outputs a file that contains the final javascript array.




  • This is the perl script that does all the work.
  • sample.csv: Sample CSV to use with the script.
  • Silverchair_SOASTA_Test_Full_student_list_site_165_testing.csv: Sample CSV provided by Silverchair. NOTE: This doesn't work with the script.
  • filename.out: This is the output file that contains the results of the execution: the javascript array.


    You use this asset on the command line in Terminal. Although not necessary, it is easiest if you put the perl script in the same directory as the CSV file you want to convert. At the terminal, type:

    perl sample.csv

    When the script runs, the output will be in filename.out.

    Note: Not all CSV files will run with this perl script. Some have different end of line characters (usually from Windows machines) that don't work properly on the Mac. If you get one (like Silverchair_SOASTA_Test_Full_student_list_site_165_testing.csv above) that doesn't work, you will need to copy the contents of the file into a new file. Save the file on the Mac and the new file will work fine.

    Perl script contents

    #!/usr/bin/perl -w
    #use strict;

    # create an output that is formatted for a javascript array
    # when source is a CSV file. Output should look like this:
    # [
    # ["","4690C47DADADA88BF8F4C6A2F262798A"],
    # ["","109A25E92070491C83C2ED3ABE96CACE"],
    # ["","754968094C842A07B663962196A776EC"]
    # ];

    # usage: perl FILENAME.csv

    print "\n---------------------- \n";
    print "NOTE: This script has only been tested with 2 columns data!! \n";
    print "---------------------- \n\n";

    open(FILE, " <$ARGV[0]") or die "can't open <$ARGV[0]: $!";
    $lines_in_file++ while ;
    # $lines_in_file now holds the number of lines read

    open(MYINPUTFILE, "<$ARGV[0]");
    open(MYOUTPUTFILE, ">filename.out");

    # start array
    print MYOUTPUTFILE "[\n";

    $count_lines = 1;
    # Good practice to store $_ value because subsequent operations may change it.
    my($line) = $_;

    # $count_commas = ($line =~ tr/,//);
    # print "There are $count_commas commas in the string: ";

    # Good practice to always strip the trailing
    # newline from the line.
    #$line=~ tr/\015//d;
    $line =~ s/\r\n$/\n/;

    # use 'split' to put each comma-separate value into an element in the
    # @line_as_array array
    @line_as_array = split(/,/, $line);
    $elements_in_array = scalar (@line_as_array);

    if($elements_in_array ne 2){
    print "found more than 2 elements in line; this is untested\n\n";
    #die "aborting"

    # print first character in line
    print MYOUTPUTFILE "[";

    $count = 1;

    foreach (@line_as_array) {
    print MYOUTPUTFILE "\"";
    print MYOUTPUTFILE $_;

    # only print a comma separator if isn't the last element on the line
    if($count ne $elements_in_array){
    print MYOUTPUTFILE "\",";}
    print MYOUTPUTFILE "\"";


    #end the line

    if($count_lines ne $lines_in_file){
    print MYOUTPUTFILE "],\n";
    print MYOUTPUTFILE "]\n";

    close MYINPUTFILE;

    # end array
    print MYOUTPUTFILE "];\n";


    print "-->Output is in filename.out\n\n";