API Post a matching quiz question creation

Jump to solution
Nva2023
Community Explorer

Hi James! @James 

I'm currently struggling with creating matching questions via API from a text file. While I'm able to create the matching  question itself, the matching values are coming through empty, likely due to an incorrect JSON format in the body. Unfortunately, the Canvas documentation doesn't provide an example for creating matching questions.

If you could share an example, it would be greatly appreciated!

Labels (1)
0 Likes
1 Solution
James
Community Champion

@Nva2023 

It would have been quicker for you to give an example and have someone provide suggestions. The problem has potentially different solutions depending on which programming language and/or library you're using to make the API calls.

In general, here's how to get the proper formatting for those tricky API calls.

  1. Open the developer tools in your browser. Switch to the network tab. Filter by fetch/xhr.
  2. Create / edit a quiz.
  3. Create a new question of the type you need.
  4. Click update
  5. Switch to the developer tools in your browser and view the request
  6. Sift through the request for the pertinent information

Here is what I get for a matching question. It had three choices (A, B, C) tied to three numbers (1, 2, 3). It had three distractors (D, E, F).

question[question_name]: Question
question[assessment_question_id]: 
question[question_type]: matching_question
question[points_possible]: 1
question[correct_comments_html]: 
question[incorrect_comments_html]: 
question[neutral_comments_html]: 
question[question_text]: <p>Match these items</p>
question[regrade_option]: 
question[position]: 0
question[text_after_answers]: 
question[matching_answer_incorrect_matches]: D
E
F
question[answers][0][0]: NaN
question[answers][0][1]: NaN
question[answers][0][2]: NaN
question[answers][0][3]: NaN
question[answers][0][4]: NaN
question[answers][0][5]: NaN
question[answers][0][6]: NaN
question[answers][0][answer_exact]: 0
question[answers][0][answer_error_margin]: 0
question[answers][0][answer_range_start]: 0
question[answers][0][answer_range_end]: 0
question[answers][0][answer_approximate]: 0
question[answers][0][answer_precision]: 10
question[answers][0][answer_weight]: 100
question[answers][0][numerical_answer_type]: exact_answer
question[answers][0][blank_id]: 
question[answers][0][id]: 
question[answers][0][match_id]: 
question[answers][0][answer_text]: 
question[answers][0][answer_match_left]: A
question[answers][0][answer_match_right]: 1
question[answers][0][answer_comment]: 
question[answers][0][answer_html]: 
question[answers][0][answer_match_left_html]: 
question[answers][0][answer_comment_html]: 
question[answers][1][0]: NaN
question[answers][1][1]: NaN
question[answers][1][2]: NaN
question[answers][1][3]: NaN
question[answers][1][4]: NaN
question[answers][1][5]: NaN
question[answers][1][6]: NaN
question[answers][1][answer_exact]: 0
question[answers][1][answer_error_margin]: 0
question[answers][1][answer_range_start]: 0
question[answers][1][answer_range_end]: 0
question[answers][1][answer_approximate]: 0
question[answers][1][answer_precision]: 10
question[answers][1][answer_weight]: 100
question[answers][1][numerical_answer_type]: exact_answer
question[answers][1][blank_id]: 
question[answers][1][id]: 
question[answers][1][match_id]: 
question[answers][1][answer_text]: 
question[answers][1][answer_match_left]: B
question[answers][1][answer_match_right]: 2
question[answers][1][answer_comment]: 
question[answers][1][answer_html]: 
question[answers][1][answer_match_left_html]: 
question[answers][1][answer_comment_html]: 
question[answers][2][0]: NaN
question[answers][2][1]: NaN
question[answers][2][2]: NaN
question[answers][2][3]: NaN
question[answers][2][4]: NaN
question[answers][2][5]: NaN
question[answers][2][6]: NaN
question[answers][2][answer_exact]: 0
question[answers][2][answer_error_margin]: 0
question[answers][2][answer_range_start]: 0
question[answers][2][answer_range_end]: 0
question[answers][2][answer_approximate]: 0
question[answers][2][answer_precision]: 10
question[answers][2][answer_weight]: 100
question[answers][2][numerical_answer_type]: exact_answer
question[answers][2][blank_id]: 
question[answers][2][id]: 
question[answers][2][match_id]: 
question[answers][2][answer_text]: 
question[answers][2][answer_match_left]: C
question[answers][2][answer_match_right]: 3
question[answers][2][answer_comment]: 
question[answers][2][answer_html]: 
question[answers][2][answer_match_left_html]: 
question[answers][2][answer_comment_html]: 
_method: POST


Most of the stuff in there is not needed. Here are the ones that sound related to matching questions.

question[question_name]: Question
question[question_type]: matching_question
question[points_possible]: 1
question[question_text]: <p>Match these items</p>
question[matching_answer_incorrect_matches]: D
E
F
question[answers][0][answer_match_left]: A
question[answers][0][answer_match_right]: 1
question[answers][1][answer_match_left]: B
question[answers][1][answer_match_right]: 2
question[answers][2][answer_match_left]: C
question[answers][2][answer_match_right]: 3

 

The numbering [0], [1], [2] is important. Depending on your language / library, it may not be added.

Let's say that you would rather use JSON than form data. I mean, who wouldn't?

For example, you might think you want this

{
  "questions": {
    "question_name": "Question",
    "question_type": "matching_question",
    "points_possible": 1,
    "question_text": "<p>Match these items.</p>",
    "matching_answers_incorrect_matches": "D\nE\nF",
    "answers": [
      { "answer_match_left": "A", "answer_match_right": 1 },
      { "answer_match_left": "B", "answer_match_right": 2 },
      { "answer_match_left": "C", "answer_match_right": 3 }
    ]
  }
}

 

However, sending that object to the API doesn't work -- at least not directly. This is why it depends on your library because some might handle it properly. Your library might modify it to be in the proper format, but that's not the right format to start with.

What you need to make that work with JSON is to convert the array into an object whose properties are the numbered keys.

{
  "questions": {
    "question_name": "Question",
    "question_type": "matching_question",
    "points_possible": 1,
    "question_text": "<p>Match these items.</p>",
    "matching_answers_incorrect_matches": "D\nE\nF",
    "answers": {
      "0": { "answer_match_left": "A", "answer_match_right": 1 },
      "1": { "answer_match_left": "B", "answer_match_right": 2 },
      "2": { "answer_match_left": "C", "answer_match_right": 3 }
    }
  }
}

 

Now, I didn't test that those calls actually create a question. There might be some other things required. I'm in the middle of something and don't have time to dig deeper. I apologize if I have any typos and I didn't verify that the newline \n would work for the distractors.

View solution in original post