The Instructure Community will enter a read-only state on November 22, 2025 as we prepare to migrate to our new Community platform in early December.
Read our blog post for more info about this change.
Found this content helpful? Log in or sign up to leave a like!
apiruby
Thanks to @jperkins and the Ruby and the Canvas API Self-paced course at https://learn.canvas.net/courses/2438 ,
I am off and running on scripting calls to API.
I am looking for help from anyone on the following questions, though (and forgive my ignorance if I misuse terms):
I am playing around with getting data.
I would like a list of courses with their term_id and number of students enrolled.
I make the call to this endpoint (with these parameters)
then parse the response to get the termid
then crank it into a csv file with the other course info (one line per course).
My script (attached) works, I just need answers to a few questions to get it to work better
1. I just played around with the "nested" JSON objects until I ferreted out the termid. (again, sorry, don't know the parlance - Inside the course JSON object , "term" : is a braced comma-separated list of its own parameters...)
Is there a better / easier way to get to these "second level" ids, names, etc than how I did it above?
Here is the course response and what I wanted
2. If I set the per_page as above (100), then the term_id only updates in my csv every 100 lines based on the first termid in that call. I think I probably need to move my script around a bit, but I have tried to no avail. If I change the per_page to =1, then it works (as it is updated every call again, but this time there is only one course per call).
This, of course is slow.
Attached is my script ( from the course, site and token redacted, annotated by me, so forgive me if I misunderstood the purpose of stuff).
Thanks for any thoughts!
Solved! Go to Solution.
Hi @dis1 ,
That looks about right, but data[0] will always be the first element (first course) in the response. If you want the terms fo a particular course, you need to read the term for that course inside the loop, where you get the rest of the course info.
You want courses['term']['id'] for each course, not data[0]['term']['id'] for the response.
Hi @dis1 ,
That looks about right, but data[0] will always be the first element (first course) in the response. If you want the terms fo a particular course, you need to read the term for that course inside the loop, where you get the rest of the course info.
You want courses['term']['id'] for each course, not data[0]['term']['id'] for the response.
Thanks, @jsavage2
A succinct answer to both my questions, and
worked a charm!
…and adds to my understanding… I was barking up the wrong loop.
I appreciate people with your knowledge being willing to share with ruby newbies.
Thanks for your reply!
Community helpTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign inTo interact with Panda Bot, our automated chatbot, you need to sign up or log in:
Sign in