In this assignment, we are pulling book review data in the JSON format. As there is a lot of meta data in the returned data, we’ll only display the relevant book data in one of the sub data structures.
Note: This script assumes an environment variable, api_key_nytimes, is set beforehand.
# Set constants
api_key <- Sys.getenv('api_key_nytimes')
api_url <- "https://api.nytimes.com/svc/books/v3/lists.json?list=hardcover-fiction"
# Build the api-key parameter
api_param <- paste('api-key', api_key, sep='=')
# Set the NYTimes API endpoint for the hard copy fiction with a GET request
url <- paste(api_url, api_param, sep="&")
# create a curl handle for connection
curl_handle <- new_handle()
handle_setopt(curl_handle, customrequest="GET")
# Connect to the API
json_request <- curl_fetch_memory(url, handle=curl_handle)
# Retrieve the request content
json_result <- fromJSON(rawToChar(json_request$content))
json_tibble <- as_tibble(json_result, validate = F)
# use the results column for the book details
books <- bind_rows( json_tibble$results$book_details )
# Display the first three rows as an example
kable(books %>% slice(1:3), caption = "NY Times Non Fiction Hardcover Titles" ) %>%
kable_styling(latex_options = "scale_down")
| title | description | contributor | author | contributor_note | price | age_group | publisher | primary_isbn13 | primary_isbn10 |
|---|---|---|---|---|---|---|---|---|---|
| STATE OF TERROR | In the wake of the previous administration’s mishandling of international affairs, the new Secretary of State Ellen Adams confronts interconnected global threats. | by Hillary Rodham Clinton and Louise Penny | Hillary Rodham Clinton and Louise Penny | 0.00 | Simon & Schuster, St. Martin’s | 9781982173678 | 198217367X | ||
| THE WISH | Maggie Dawes, a renowned travel photographer, struggles with a medical diagnosis over Christmas. | by Nicholas Sparks | Nicholas Sparks | 0.00 | Grand Central | 9781538728628 | 1538728621 | ||
| THE LINCOLN HIGHWAY | Two friends who escaped from a juvenile work farm take Emmett Watson on an unexpected journey to New York City in 1954. | by Amor Towles | Amor Towles | 0.00 | Viking | 9780735222359 | 0735222355 |