in utility_scripts/csv2json_converter/js/qnabot_csv2json_converter.js [45:92]
function convertToQnABotJSON(arrCSVInput) {
var strJSONOutput = '{"qna": [{'; //start the JSON structure
updateProgress ("<br>Number of rows found (including header row): " + arrCSVInput.length);
updateProgress ("<br>Conversion to JSON in progress...");
//check if the input csv file has the needed fields. If not, return exception and stop conversion
if (!checkCSVFileFormat(arrCSVInput[0])) {
return false;
}
for (var intRow = 1; intRow < (arrCSVInput.length); intRow++) { //iterate through the array object (skip header row)
if (intRow > 1) {
strJSONOutput = strJSONOutput + '}, {';
}
for (var intCol = 0; intCol <= arrCSVInput[intRow].length; intCol++){ //iterate through each array column for each row
if (intCol == QUESTION_IDENTIFIER_INDEX) {
strJSONOutput = strJSONOutput + create_QnA_qid(arrCSVInput[intRow][intCol]); //create the QnA qid JSON key
}
if (intCol == QUESTION_TYPE_INDEX) {
strJSONOutput = strJSONOutput + create_QnA_type(arrCSVInput[intRow][intCol]); //create the QnA type JSON key
}
if (intCol == QUESTION_INDEX) {
strJSONOutput = strJSONOutput + create_QnA_q(arrCSVInput[intRow][intCol]); //create the QnA q JSON key
}
if (intCol == QUESTION_ANSWER_INDEX) {
strJSONOutput = strJSONOutput + create_QnA_a(arrCSVInput[intRow][intCol]); //create the QnA a JSON key
}
if (intCol == QUESTION_ANSWER_MARKDOWN && arrCSVInput[intRow][intCol]) { //create the QnA alt+markdown JSON key
strJSONOutput = strJSONOutput + create_QnA_alt_markdown(arrCSVInput[intRow][intCol]);
}
if (arrCSVInput[intRow][intCol] && intCol < arrCSVInput[intRow].length-1) { //add a comma if more JSON key pairs are to be added
strJSONOutput = strJSONOutput + ', ';
}
}
}
strJSONOutput = strJSONOutput + '}]}'; //close the JSON structure
strJSONOutput = strJSONOutput.replace(/""/g, '"'); //replace globally for any occurence of two double-quotes
try {
if (JSON.parse(strJSONOutput) && typeof JSON.parse(strJSONOutput) === "object") {
saveConvertedFile (strJSONOutput); //create converted file and provide dialog box to save the file
}
} catch (e){
console.log (e);
updateProgress ("<br><font color='red'>ERROR: Input file does not meet file format specifications. <br> Please check your input file headers and/or remove any end-of-line commas and try again. </font>");
}
return (strJSONOutput);
}