Goodreads Developers discussion

242 views
feature requests > review_list shelf return issue

Comments Showing 1-2 of 2 (2 new)    post a comment »
dateDown arrow    newest »

message 1: by Brad (new)

Brad Dickason | 16 comments Hey Goodreads devs,

First of all, thanks for taking the time to build and support developers with an API! I'm still refreshing myself on how to code after a few years and it's been super helpful to learn by experimenting!

I've been poking around the review_list API, specifically to allow a user to browse their shelves once they've OAuth'd. I ran into a really rough issue that due to some shortcomings in the templating engine I'm using caused me to run around in circles and pull my hair out.

I'm working in NodeJS with the Express framework and I'm pulling the feed in XML as I haven't seen a JSON variant. Once I make a call to the individual shelf (example: http://www.goodreads.com/review/list/...) I convert it to JSON, and iterate through the books.book object with an 'each.'

The hangup that's giving me trouble is that upon converting the XML to JSON, I'm seeing an Array of books (books.book[]) if more than one book is included in the response, but a simple js object (books.book) if only one book exists. This breaks my each loop and causes a bunch of other issues with my app at the moment.

I've implemented a simple workaround right now to check whether or not books.book is an array and avoid the loop otherwise, but this is obviously not an ideal solution.

Is it possible that the book object could be returned as an object regardless of whether there are one or many? Or is this simply a limitation of converting XML to JSON?

I've created a simple gist so you can see my basic dilemma at the moment: https://gist.github.com/1068939

Any advice/help would be appreciated!


message 2: by Craig (new)

Craig (cpjolicoeur) | 29 comments Brad, that type of API result is actually pretty common of most webservice APIs you will find around the Internet.

You have 2 solutions, first continue what you are doing and check for the object being an array and branch accordingly.

Or you can also force the returned object into an array and them just flatten that new array into a 1-dimensional array. Then you only need a single code path that loops through an array since you know it will always now be an array object.


back to top