- Is to choose one of the New York Times APIs, construct an inferface in R to read in the JSON data,
transform it into an R DataFrame.
library(httr)
library(jsonlite)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.1 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ purrr::flatten() masks jsonlite::flatten()
## ✖ dplyr::lag() masks stats::lag()
We will be using the Books API for this assingment
api_url <- "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?api-key=kFTFTG3izBIGadVV6JG6Nu72ti5CNuhj"
response <- GET(api_url)
content <- content(response, "text")
json_data <- fromJSON(content)
json_df <- json_data$results
glimpse(json_df)
## Rows: 20
## Columns: 11
## $ title <chr> "\"I GIVE YOU MY BODY ...\"", "\"MOST BLESSED OF THE …
## $ description <chr> "The author of the Outlander novels gives tips on wri…
## $ contributor <chr> "by Diana Gabaldon", "by Annette Gordon-Reed and Pete…
## $ author <chr> "Diana Gabaldon", "Annette Gordon-Reed and Peter S On…
## $ contributor_note <chr> "", "", "", "", "", "", "", "", "", NA, NA, NA, "", "…
## $ price <chr> "0.00", "0.00", "0.00", "0.00", "0.00", "0.00", "0.00…
## $ age_group <chr> "", "", "", "", "", "", "", "", "", NA, NA, NA, "", "…
## $ publisher <chr> "Dell", "Liveright", "Beacon", "HarperCollins", "Port…
## $ isbns <list> [<data.frame[1 x 2]>], [<data.frame[1 x 2]>], [<data…
## $ ranks_history <list> [<data.frame[1 x 11]>], [<data.frame[1 x 11]>], [<da…
## $ reviews <list> [<data.frame[1 x 4]>], [<data.frame[1 x 4]>], [<data…
class(json_df)
## [1] "data.frame"
library(DT)
datatable(json_df)