Task

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.

Loading Packages

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(httr)
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:purrr':
## 
##     flatten
library(dplyr)

GET WEB API - CURRENT BESTSELLER HARDCOVER BOOKS

#Get data using URL
NYT_cur_books_url <- GET("https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=kRCHVmjeEhGbxnLq5GW3GAr5rvLPrtCn")

# Parse JSON content
NYT_popular_Books <- content(NYT_cur_books_url, as = 'text') 

#in json format
NYT_popular_books2 <- fromJSON(NYT_popular_Books)

# Converting JSON to DataFrame
NYT_popular_books2   <- NYT_popular_books2$results

##only selecting needed columns
NYT_popular_cur_books <- subset(NYT_popular_books2$books , select = c(title, author, rank, amazon_product_url))

#confirm data frame
class(NYT_popular_cur_books)
## [1] "data.frame"

Final Results

#display  of the dataframe
head(NYT_popular_cur_books)
##                              title             author rank
## 1                        THE WOMEN     Kristin Hannah    1
## 2                      FOURTH WING     Rebecca Yarros    2
## 3                       IRON FLAME     Rebecca Yarros    3
## 4             EMPIRE OF THE DAMNED       Jay Kristoff    4
## 5            A FATE INKED IN BLOOD Danielle L. Jensen    5
## 6 THE HEAVEN & EARTH GROCERY STORE      James McBride    6
##                                     amazon_product_url
## 1 https://www.amazon.com/dp/1250178630?tag=NYTBSREV-20
## 2 https://www.amazon.com/dp/1649374046?tag=NYTBSREV-20
## 3 https://www.amazon.com/dp/1649374178?tag=NYTBSREV-20
## 4 https://www.amazon.com/dp/1250245338?tag=NYTBSREV-20
## 5 https://www.amazon.com/dp/0593599837?tag=NYTBSREV-20
## 6 https://www.amazon.com/dp/0593422945?tag=NYTBSREV-20