Library

library(httr)
library(jsonlite)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)

Introduction

The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis You’ll need to start by signing up for an API key. 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.

Here I chose Book API, which provides information about book reviews and The New York Times best seller list.

Store API Key

# Store the API Key securely
file.edit("~/.Renviron")
api_key <- Sys.getenv("nyt_api_key")
if (api_key == "") {
  stop("API Key not found.")
}

Transform from JSON to R Dataframe

best_sellers <- paste0("https://api.nytimes.com/svc/books/v3/lists/full-overview.json?api-key=", api_key)

response <- GET(best_sellers)
if (http_status(response)$category != "Success") {
  stop("Failed to retrive data.")
}

books_content<- content(response, as = "text", encoding = "UTF-8")

books_data <- fromJSON(books_content)

books_df <- books_data$results$lists %>%
  unnest(col = c(books)) %>%
  as.data.frame()

glimpse(books_df)
## Rows: 140
## Columns: 34
## $ list_id              <int> 704, 704, 704, 704, 704, 704, 704, 704, 704, 704,…
## $ list_name            <chr> "Combined Print and E-Book Fiction", "Combined Pr…
## $ list_name_encoded    <chr> "combined-print-and-e-book-fiction", "combined-pr…
## $ display_name         <chr> "Combined Print & E-Book Fiction", "Combined Prin…
## $ updated              <chr> "WEEKLY", "WEEKLY", "WEEKLY", "WEEKLY", "WEEKLY",…
## $ list_image           <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ list_image_width     <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ list_image_height    <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
## $ age_group            <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ amazon_product_url   <chr> "https://www.amazon.com/dp/059344129X?tag=thenewy…
## $ article_chapter_link <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ author               <chr> "Emily Henry", "Navessa Allen", "Jeneva Rose", "R…
## $ book_image           <chr> "https://storage.googleapis.com/du-prd/books/imag…
## $ book_image_width     <int> 331, 333, 437, 310, 331, 328, 333, 128, 309, 329,…
## $ book_image_height    <int> 500, 500, 500, 500, 500, 500, 500, 195, 500, 500,…
## $ book_review_link     <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ book_uri             <chr> "nyt://book/9acef644-540c-598c-8d0b-abe49ecc78c0"…
## $ btrn                 <chr> "242061668", "235511789", "242572337", "235758722…
## $ contributor          <chr> "by Emily Henry", "by Navessa Allen", "by Jeneva …
## $ contributor_note     <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ created_date         <chr> "2025-04-30 22:20:29", "2025-04-30 22:20:29", "20…
## $ description          <chr> "A writer looking for her big break competes agai…
## $ first_chapter_link   <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ price                <chr> "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "…
## $ primary_isbn10       <chr> "059344129X", "None", "None", "None", "1538742055…
## $ primary_isbn13       <chr> "9780593441299", "9781638933359", "9798874620936"…
## $ publisher            <chr> "Berkley", "Slowburn", "Blackstone", "Red Tower",…
## $ rank                 <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15…
## $ rank_last_week       <int> 0, 15, 1, 4, 2, 5, 0, 9, 6, 7, 0, 8, 0, 0, 14, 0,…
## $ sunday_review_link   <chr> "", "", "", "", "", "", "", "", "", "", "", "", "…
## $ title                <chr> "GREAT BIG BEAUTIFUL LIFE", "LIGHTS OUT", "THE PE…
## $ updated_date         <chr> "2025-04-30 22:24:55", "2025-04-30 22:24:55", "20…
## $ weeks_on_list        <int> 1, 15, 2, 14, 2, 4, 1, 79, 92, 8, 1, 94, 1, 46, 2…
## $ buy_links            <list> [<data.frame[5 x 2]>], [<data.frame[5 x 2]>], [<…

Clean and Tidy Data For Top 10 Fiction & Non-Fiction

# Clean data
clean_books_df <- books_df %>%
  select(list_name, title, author, rank, rank_last_week, weeks_on_list, description, publisher, book_image, buy_links)
head(clean_books_df)
##                           list_name                    title         author
## 1 Combined Print and E-Book Fiction GREAT BIG BEAUTIFUL LIFE    Emily Henry
## 2 Combined Print and E-Book Fiction               LIGHTS OUT  Navessa Allen
## 3 Combined Print and E-Book Fiction      THE PERFECT DIVORCE    Jeneva Rose
## 4 Combined Print and E-Book Fiction               ONYX STORM Rebecca Yarros
## 5 Combined Print and E-Book Fiction        STRANGERS IN TIME David Baldacci
## 6 Combined Print and E-Book Fiction   SAY YOU'LL REMEMBER ME     Ab Jimenez
##   rank rank_last_week weeks_on_list
## 1    1              0             1
## 2    2             15            15
## 3    3              1             2
## 4    4              4            14
## 5    5              2             2
## 6    6              5             4
##                                                                                                                                                                                                description
## 1                                                                          A writer looking for her big break competes against a Pulitzer winner to tell the story of an octogenarian with a storied past.
## 2                                                                                                        As Aly and Josh live out their dark fantasies, someone with sinister intentions impinges on them.
## 3 Around the time of the disappearance of a woman who had a one-night stand with the attorney Sarah Morgan’s new husband, police reopen the investigation into the murder of her first husband’s mistress.
## 4                                                                    The third book in the Empyrean series. As enemies gain traction, Violet Sorrengail goes beyond the Aretian wards in search of allies.
## 5                                                                                        In London in 1944, two teenagers dealing with the dangers of World War II form an alliance with a bookshop owner.
## 6                                                                                           Despite having a great date, Samantha asks a veterinarian to forget about her because her family is in crisis.
##       publisher
## 1       Berkley
## 2      Slowburn
## 3    Blackstone
## 4     Red Tower
## 5 Grand Central
## 6       Forever
##                                                             book_image
## 1 https://storage.googleapis.com/du-prd/books/images/9780593441299.jpg
## 2 https://storage.googleapis.com/du-prd/books/images/9781638932239.jpg
## 3 https://storage.googleapis.com/du-prd/books/images/9798874620936.jpg
## 4 https://storage.googleapis.com/du-prd/books/images/9781649374189.jpg
## 5 https://storage.googleapis.com/du-prd/books/images/9781538742051.jpg
## 6 https://storage.googleapis.com/du-prd/books/images/9781538759196.jpg
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                buy_links
## 1  Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/059344129X?tag=thenewyorktim-20, https://goto.applebooks.apple/9780593441299?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9780593441299, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FGREAT%2BBIG%2BBEAUTIFUL%2BLIFE%2FEmily%2BHenry%2F9780593441299, https://bookshop.org/a/3546/9780593441299
## 2                  Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/1638932239?tag=thenewyorktim-20, https://goto.applebooks.apple/9781638933359?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781638933359, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FLIGHTS%2BOUT%2FNavessa%2BAllen%2F9781638933359, https://bookshop.org/a/3546/9781638933359
## 3         Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/B0DGY5F5JX?tag=thenewyorktim-20, https://goto.applebooks.apple/9798874620936?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9798874620936, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FTHE%2BPERFECT%2BDIVORCE%2FJeneva%2BRose%2F9798874620936, https://bookshop.org/a/3546/9798874620936
## 4                 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/1649374186?tag=thenewyorktim-20, https://goto.applebooks.apple/9781649376947?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781649376947, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FONYX%2BSTORM%2FRebecca%2BYarros%2F9781649376947, https://bookshop.org/a/3546/9781649376947
## 5        Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/1538742055?tag=thenewyorktim-20, https://goto.applebooks.apple/9781538742051?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781538742051, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FSTRANGERS%2BIN%2BTIME%2FDavid%2BBaldacci%2F9781538742051, https://bookshop.org/a/3546/9781538742051
## 6 Amazon, Apple Books, Barnes and Noble, Books-A-Million, Bookshop.org, https://www.amazon.com/dp/1538759195?tag=thenewyorktim-20, https://goto.applebooks.apple/9781538759196?at=10lIEQ, https://www.anrdoezrs.net/click-7990613-11819508?url=https%3A%2F%2Fwww.barnesandnoble.com%2Fw%2F%3Fean%3D9781538759196, https://www.anrdoezrs.net/click-7990613-35140?url=https%3A%2F%2Fwww.booksamillion.com%2Fp%2FSAY%2BYOU%2527LL%2BREMEMBER%2BME%2FAb%2BJimenez%2F9781538759196, https://bookshop.org/a/3546/9781538759196
# Find Top-Ranked Fictions
top_10_fiction <- clean_books_df %>%
  filter(list_name == "Combined Print and E-Book Fiction") %>%
  arrange(rank) %>%
  slice_head(n = 10) %>%
  select(rank, list_name, title)
top_10_fiction
##    rank                         list_name                           title
## 1     1 Combined Print and E-Book Fiction        GREAT BIG BEAUTIFUL LIFE
## 2     2 Combined Print and E-Book Fiction                      LIGHTS OUT
## 3     3 Combined Print and E-Book Fiction             THE PERFECT DIVORCE
## 4     4 Combined Print and E-Book Fiction                      ONYX STORM
## 5     5 Combined Print and E-Book Fiction               STRANGERS IN TIME
## 6     6 Combined Print and E-Book Fiction          SAY YOU'LL REMEMBER ME
## 7     7 Combined Print and E-Book Fiction 2 SISTERS MURDER INVESTIGATIONS
## 8     8 Combined Print and E-Book Fiction                 THE NIGHTINGALE
## 9     9 Combined Print and E-Book Fiction                     FOURTH WING
## 10   10 Combined Print and E-Book Fiction                  BROKEN COUNTRY
#Find Top-Ranked Non-Fictions
top_10_nonfiction <- clean_books_df %>%
  filter(list_name == "Combined Print and E-Book Nonfiction") %>%
  arrange(rank) %>%
  slice_head(n = 10) %>%
  select(rank, list_name, title)
top_10_nonfiction
##    rank                            list_name                          title
## 1     1 Combined Print and E-Book Nonfiction                      MATRIARCH
## 2     2 Combined Print and E-Book Nonfiction                      ABUNDANCE
## 3     3 Combined Print and E-Book Nonfiction     EVERYTHING IS TUBERCULOSIS
## 4     4 Combined Print and E-Book Nonfiction                  NOTES TO JOHN
## 5     5 Combined Print and E-Book Nonfiction                           HOPE
## 6     6 Combined Print and E-Book Nonfiction         THE ANXIOUS GENERATION
## 7     7 Combined Print and E-Book Nonfiction                     ON TYRANNY
## 8     8 Combined Print and E-Book Nonfiction       THE BODY KEEPS THE SCORE
## 9     9 Combined Print and E-Book Nonfiction             WHO IS GOVERNMENT?
## 10   10 Combined Print and E-Book Nonfiction ON DEMOCRACIES AND DEATH CULTS

Conclusion

For this assignment, we aiming to transform JSON format data from The New York Time API into R data frame. As well as to have ability to continuously working with the data.