Your task is to choose one of the New York Times APIs, construct an interface in R to read in the JSON data, and transform it into an R DataFrame.
I pulled data from a list of the current hardcover fiction book and transformed it into an R Data Frame. Since the data frame was large, I only included a glimpse of it.
#data frame for hardcover fiction books
fiction <- fromJSON("https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=jl72XOQZLZ2FAL1K0h054QyKHUA2Xizm") %>%
with(results) %>%
with(books) %>%
as.data.frame()
glimpse(fiction)
## Rows: 15
## Columns: 26
## $ rank <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
## $ rank_last_week <int> 0, 1, 5, 7, 4, 8, 3, 2, 10, 0, 12, 0, 11, 9, 13
## $ weeks_on_list <int> 1, 3, 2, 5, 2, 4, 2, 2, 20, 1, 111, 1, 4, 6, 19
## $ asterisk <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ dagger <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ primary_isbn10 <chr> "0385545967", "1538728575", "073522465X", "052...
## $ primary_isbn13 <chr> "9780385545969", "9781538728574", "97807352246...
## $ publisher <chr> "Doubleday", "Grand Central", "Viking", "Vikin...
## $ description <chr> "The third book in the Jake Brigance series. A...
## $ price <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
## $ title <chr> "A TIME FOR MERCY", "THE RETURN", "THE SEARCHE...
## $ author <chr> "John Grisham", "Nicholas Sparks", "Tana Frenc...
## $ contributor <chr> "by John Grisham", "by Nicholas Sparks", "by T...
## $ contributor_note <chr> "", "", "", "", "", "", "", "", "", "", "", ""...
## $ book_image <chr> "https://s1.nyt.com/du/books/images/9780385545...
## $ book_image_width <int> 329, 329, 331, 329, 331, 329, 329, 322, 331, 3...
## $ book_image_height <int> 500, 500, 500, 500, 500, 500, 500, 500, 500, 5...
## $ amazon_product_url <chr> "https://www.amazon.com/dp/0385545967?tag=NYTB...
## $ age_group <chr> "", "", "", "", "", "", "", "", "", "", "", ""...
## $ book_review_link <chr> "", "", "", "", "", "", "", "", "", "", "", ""...
## $ first_chapter_link <chr> "", "", "", "", "", "", "", "", "", "", "", ""...
## $ sunday_review_link <chr> "", "", "", "", "", "", "", "", "", "", "", ""...
## $ article_chapter_link <chr> "", "", "", "", "", "", "", "", "", "", "", ""...
## $ isbns <list> [<data.frame[2 x 2]>, <data.frame[5 x 2]>, <d...
## $ buy_links <list> [<data.frame[6 x 2]>, <data.frame[6 x 2]>, <d...
## $ book_uri <chr> "nyt://book/33a48cf6-d7f3-5113-aa1e-6adcbb3853...
There is a list that we can grab that includes the types of books which each have their own list of best selling books. Hardcover fiction was only one type. You can insert the encoded list name into the url and run it through a for loop to create a data frame for each.
#creating a data frame with lists
list_name_encoded <- fromJSON("https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=jl72XOQZLZ2FAL1K0h054QyKHUA2Xizm") %>%
with(results) %>%
select(list_name_encoded) %>%
as.data.frame()
a <- "https://api.nytimes.com/svc/books/v3/lists/current/"
b <- ".json?api-key=jl72XOQZLZ2FAL1K0h054QyKHUA2Xizm"
#creating urls
list_name_encoded <- list_name_encoded %>%
mutate(list_name_encoded = paste0(a,list_name_encoded,b)) %>%
pull()
list_name_encoded <- list_name_encoded %>%
as.list()
j=1
for loop to create a data frame for each list
for(i in 1:length(list_name_encoded)){
list_name_encoded[[j]]= fromJSON(list_name_encoded[[j]]) %>%
with(results) %>%
with(books) %>%
as.data.frame()
Sys.sleep(5)
j=j+1
}
Finding the list for best-selling hardcover fiction books and transforming it ran quickly. However, running the for loop to create a data frame for each list takes a while to run.