I am working on a form that will send data from the google forms to the real-time database. However, I am having a hard time setting the data in JSON format (fairly new to this).
I would like to format it like the following below (data there are examples):
"QuizName": {
"1": {
"question-type": "multiple-choice",
"question": "Find the odd one out?",
"option1": "FTP",
"option2": "POP",
"option3": "TCP",
"answer": "POP",
"date-added": "10/06/2021"
},
"2": {
"question-type": "checkbox",
"question": "Which of the following are not planets?",
"option1": "Earth",
"option2": "Sun",
"option3": "Jupiter",
"option4": "Mars",
"option5": "Pluto",
"answer": "Sun, Pluto",
"date-added": "10/06/2021"
}
"3": {
"question-type": "Input-text",
"question": "In your own words, describe Mitosis?",
"user-input": "Mitosis is a process where a single cell divides into 2 identical daughter cells",
"answer": "user-input",
"date-added": "10/06/2021"
}
}
The code below is where I extract the form questions and answers which calls the sendExtractedData. From there, I would like to pass in the values (the parameters are arrays except for the form title parameter).
function extractFormData(){
var form = FormApp.getActiveForm();
var form_title = DriveApp.getFileById(form.getId()).getName();
var items = form.getItems();
var number_of_questions = items.length;
var question;
var answers;
var answer;
var item_type = [];
var question_title = [];
var questions = [];
var correctanswer = [];
for (var i = 0; i < items.length; i++) {
var item = items[i];
switch(item.getType()) {
case FormApp.ItemType.MULTIPLE_CHOICE:
item_type.push(item.getType());
question = item.asMultipleChoiceItem();
answers = question.getChoices();
question_title.push(question.getTitle());
console.log("Question: " + question.getTitle());
console.log("Number of answers: " + question.getChoices().length);
for (var j = 0; j < answers.length; j++) {
answer = answers[j];
questions.push(answer.getValue());
correctanswer.push(answer.isCorrectAnswer());
console.log(answer.getValue());
console.log("Is correct answer? " + answer.isCorrectAnswer());
}
break;
case FormApp.ItemType.CHECKBOX:
item_type.push(item.getType());
question = item.asCheckboxItem();
answers = question.getChoices();
question_title.push(question.getTitle());
console.log("Question: " + question.getTitle());
console.log("Number of answers: " + question.getChoices().length);
for (var k = 0; k < answers.length; k++) {
answer = answers[k];
questions.push(answer.getValue());
correctanswer.push(answer.isCorrectAnswer());
console.log(answer.getValue());
console.log("Is correct answer? " + answer.isCorrectAnswer());
}
break;
case FormApp.ItemType.PARAGRAPH_TEXT:
item_type.push(item.getType());
question = item.asParagraphTextItem();
question_title.push(question.getTitle());
console.log("Question: " + question.getTitle());
break;
case FormApp.ItemType.TEXT:
item_type.push(item.getType());
qustion = item.asTextItem();
question_title.push(question.getTitle());
console.log("Question: " + question.getTitle());
break;
}
}
var form_questions = unique(question_title);
sendExtractedData(form_title, item_type.join(', '), form_questions.join(', '), questions.join(', '), correctanswer.join(', '));
}
function sendExtractedData(form_title, typeofquestion, question, typeofanswers, correctanswer) {
var quizDatabase = FirebaseApp.getDatabaseByUrl('URL REALTIME DATABASE');
var newDate = new Date();
var dateAdded = newDate.toLocaleString("en-US");
var dataToExport = { };
//quizDatabase.setData("nameofdatabase/"+ form_title +"/", dataToExport);
}
Any help would be appreciated.