Search code examples
javascriptjquerycsvpapaparse

Check if CSV headers match, if they match continue parsing otherwise stop


I am currently trying to add some validation of some description in regards to how I want a particular .CSV format to be before it continues parsing using PapaParse.

So my idea was to check the headers first and if they equate to the following:

Extension, company, name

Then continue parsing otherwise I can throw back an error message stating the format was wrong.

All the parsing is done using PapaParse.

Ave had no joy with it but below is the current code:

var result = [];

$("#CSV-Upload").click(function () {
        $("input[type=file]").parse({
            config : {
                header : true,
                skipEmptyLines : true,
                complete : function (results, file) {
                    console.log("This file done:", file, results);
                    var string = JSON.stringify(results['data']);
                    result.push(string);
                    console.log("CSV Array: " + string);
                }
            },
            complete : function () {
                console.log("All files done!");
            }
        });
        $("#csv-file").val('');
    });

Solution

  • If I understand correctly you want to check if the certain key is present in the header. To do so using papa parse, I suggest using streaming. In papa parse the concept of streaming is to process the data as parser is reading them.

    Basically, you will check for certain key in the row object returned in the step function. Check out the following code:

    var allKeyPresent = false; // Flag
    
    Papa.parse(file, {
        header : true,
        skipEmptyLines : true,
        step: function(row, parser){
            if (!allKeyPresent) { //Only chek if flag is not set, i.e, for the first time
                parser.pause(); // pause the parser
                var first_row_data = row.data[0];
                // Now check object keys, if it match
                if (('Extension' in first_row_data) && ('name' in first_row_data) && ('email' in first_row_data)) {
                    //every required key is present
                    allKeyPresent = true;
    
                    // Do your data processing here
    
                    parser.resume();
                } else{
                    //some key is missing, abort parsing
                    parser.abort();
                }
    
            } else{ // we already match the header, all required key is present
    
                // Do the Data processing here
    
            }
    
        }
    });
    

    To know more about streaming in papa parse check out this. Also, see more about the step function in the config explanation section of the documentation.

    I hope this helps. Let me know if you have any other related query.