AnsweredAssumed Answered

Accessing JSON "2nd tier" id; Ruby script adjustment

Question asked by HCCSC Data Specialist - NVickrey on Mar 30, 2019
Latest reply on Apr 1, 2019 by HCCSC Data Specialist - NVickrey



Thanks to Jeremy Perkins and the Ruby and the Canvas API Self-paced course at ,


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


data = JSON.parse(response.body)
term = data[0]['term']['id']


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!