Found this content helpful? Log in or sign up to leave a like!
API id datatype inconsistencies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Why is the datatype for the answer.id number (without quotes) for true_false_question and string (with quotes) for short_answer_question and multiple_answers_question???
It is inconsistencies like this that make working with the API a nightmare.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Trust me that the API has much bigger headaches than this one.
I use JavaScript for most of my code and it's not an issue unless you're trying to do a strict comparison, such as with the === operator. Personally, when I know the value is an integer disguising as a string, I use parseInt to convert it to an integer and then do a strict comparison. I know that I could use == in JavaScript and it would work just fine, but I prefer strict comparisons. I also do a lot of hash work where the key is the integer, but JavaScript returns it as a string so I have to convert it back to an integer for my comparisons.
Other languages require objects of the same type to do comparisons. It sounds like you are using one of those languages. A quick search says that Ruby and Python fall into that category. For those, you would need to convert integers to strings or strings to integers to compare them. That means that you need to do the conversion, similar to what I do.
For the answer ID field, you should convert the value to an integer rather than the integers to a string. Internally, answer IDs are stored as integers for all of the question types. If you look the canvas__quiz_questions table in Canvas Data, the question_data property is a JSON object where the IDs are integers. If you look at the Quiz Answer Formats appendix, you will see all kinds of warnings about the answer IDs need to be integers.
As for other techniques, the first page of the REST API documentation may provide some insight (but not a full explanation in this case). In summary, all integers are 64-bit integers. String IDs are also used. You can force all IDs to strings with a request Accept header. When passing Boolean parameters with JSON, a literal true/false is preferred rather than as a string.
It sounds like you could use the request header to get the values as strings and let Canvas do the conversion instead of you. However, since the other question types return an integer ID, I would choose to force all of the IDs to integers since I don't want to write code to handle them separately.