BACKGROUND

The purpose of this assignment 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.


CHOOSE NY TIMES API

I elected the NY Times Bestseller List API, and after setting up an account and getting an API key, I utilized the URL listed on the NYT Developer Books API page and my API key to access the data therein:

#Setup URL with the category of interest (current non-fiction best sellers) and my API key
url <- "https://api.nytimes.com/svc/books/v3/lists/current/hardcover-nonfiction.json?api-key=oLqE3aNegIVNa1tfiAJETm7wZ8LSalGG"

After Google-searching how to do enter this URL properly (path, query, API key), I verified that there was data at the path by entering it into the browser.

READ IN JSON DATA

Once verified, I set out to GET the json response (the payload returned by a web service for what we’ve requested), retrieve the contents as a character vector, and read the content in JSON format and de-serialize it into R objects:

resp <- GET(url)
content <- content(resp, "text")
book_data <- fromJSON(content)

#book_data displays as a dataframe but is actually a list
#book_data
typeof(book_data)
## [1] "list"

TRANSFORM INTO DATAFRAME

In displaying book_data it’s displayed as a dataframe even though the type of book_data is list, so we set out to convert the list to a data frame with our variables of interest (rank, title, author, and amazon url):

select_results <- cbind(book_data$results$books$rank, book_data$results$books$title, book_data$results$books$author, book_data$results$books$amazon_product_url)

select_df <- as.data.frame(select_results)

#Rename column titles 
names(select_df)[names(select_df) == "V1"] <- "Rank"
names(select_df)[names(select_df) == "V2"] <- "Title"
names(select_df)[names(select_df) == "V3"] <- "Author"
names(select_df)[names(select_df) == "V4"] <- "Amazon URL"

select_df
##    Rank                                 Title
## 1     1                                 CASTE
## 2     2                     IS THIS ANYTHING?
## 3     3                               UNTAMED
## 4     4                   KILLING CRAZY HORSE
## 5     5                              BLACKOUT
## 6     6                                  RAGE
## 7     7                                HUMANS
## 8     8                       AMERICAN CRISIS
## 9     9               HOW TO BE AN ANTIRACIST
## 10   10 TEN LESSONS FOR A POST-PANDEMIC WORLD
## 11   11           THE MEANING OF MARIAH CAREY
## 12   12         TRUMPTY DUMPTY WANTED A CROWN
## 13   13                THE 99% INVISIBLE CITY
## 14   14                         ONE VOTE AWAY
## 15   15             TOO MUCH AND NEVER ENOUGH
##                                     Author
## 1                         Isabel Wilkerson
## 2                           Jerry Seinfeld
## 3                            Glennon Doyle
## 4          Bill O'Reilly and Martin Dugard
## 5                            Candace Owens
## 6                             Bob Woodward
## 7                          Brandon Stanton
## 8                             Andrew Cuomo
## 9                            Ibram X Kendi
## 10                          Fareed Zakaria
## 11 Mariah Carey with Michaela Angela Davis
## 12                            John Lithgow
## 13           Roman Mars and Kurt Kohlstedt
## 14                                Ted Cruz
## 15                            Mary L Trump
##                                                                         Amazon URL
## 1                https://www.amazon.com/dp/0593230256?tag=NYTBSREV-20&tag=NYTBS-20
## 2                https://www.amazon.com/dp/1982112697?tag=NYTBSREV-20&tag=NYTBS-20
## 3                https://www.amazon.com/dp/1984801252?tag=NYTBSREV-20&tag=NYTBS-20
## 4                https://www.amazon.com/dp/1627797041?tag=NYTBSREV-20&tag=NYTBS-20
## 5                https://www.amazon.com/dp/1982133279?tag=NYTBSREV-20&tag=NYTBS-20
## 6                https://www.amazon.com/dp/198213173X?tag=NYTBSREV-20&tag=NYTBS-20
## 7                https://www.amazon.com/dp/1250114292?tag=NYTBSREV-20&tag=NYTBS-20
## 8                https://www.amazon.com/dp/0593239261?tag=NYTBSREV-20&tag=NYTBS-20
## 9  https://www.amazon.com/How-Be-Antiracist-Ibram-Kendi/dp/0525509283?tag=NYTBS-20
## 10               https://www.amazon.com/dp/0393542130?tag=NYTBSREV-20&tag=NYTBS-20
## 11               https://www.amazon.com/dp/1250164680?tag=NYTBSREV-20&tag=NYTBS-20
## 12               https://www.amazon.com/dp/1797209469?tag=NYTBSREV-20&tag=NYTBS-20
## 13               https://www.amazon.com/dp/0358126606?tag=NYTBSREV-20&tag=NYTBS-20
## 14               https://www.amazon.com/dp/1684511348?tag=NYTBSREV-20&tag=NYTBS-20
## 15               https://www.amazon.com/dp/1982141468?tag=NYTBSREV-20&tag=NYTBS-20

CONCLUSION

From the above output, we can note the extracted Top 15 NYT’s current non-fiction best sellers (as of 10/23/2020). It’s interesting to observe the proportion of political titles.

We’re in the midst of a pandemic and an election season and it seems that more folks are focused on politics. The hope is that we’re arming ourselves with less polarizing of information and that we focus on pulling together rather than dividing further.

Time will tell …