For this assignment, I will use the NYT Book Reviews API to retrieve information for books written by Christa Wolf.
library(RCurl)
## Loading required package: bitops
library(rjson)
When we call the JSON data, we will first transform it into a nested list. Data for each reviewed book has is stored in its own list, so we will run a double for loop to calleach piece of data from each listed book and store it in a more convenient format - a dataframe.
callAPI <- getURL("http://api.nytimes.com/svc/books/v3/reviews.json?author=Christa%20Wolf&api-key=7d713de2214ccef5898be3e678f7ca2b:13:69774814")
jsonList <- fromJSON(callAPI) # Turn JSON data into a list
jsonList <- jsonList$results # Cut through the nested list and narrow down to the results list
christaWolf <- data.frame() #initialize dataframe
Run the loops:
for (i in 1:length(jsonList)){
for(j in 1:length(jsonList[[1]])){
christaWolf[i,j] <- jsonList[[i]][[j]]
}
}
Now we will rename the columns of our dataframe:
ourHeaders <- names(jsonList[[1]])
colnames(christaWolf) <- ourHeaders
christaWolf[,c(2,4,5,7)] # call part of data frame
## publication_dt book_title
## 1 2013-02-24 City of Angels: Or, The Overcoat of Dr. Freud
## 2 1982-09-06 No Place on Earth
## 3 1984-07-31 Cassandra: A Novel and Four Essays
## 4 1984-09-09 Cassandra: A Novel and Four Essays
## 5 1993-03-31 The Author's Dimension: Selected Essays
## book_author isbn13
## 1 Christa Wolf 9780374534295
## 2 Christa Wolf 9780374222987
## 3 Christa Wolf 9780374119560
## 4 Christa Wolf 9780374119560
## 5 Christa Wolf 9780374123024