#install.packages('tidyverse')
#install.packages('jsonlite')
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(jsonlite)
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:purrr':
## 
##     flatten
library(httr)
my_api_keys = "UzqBA5xShuTAZ3Sg9AuACli4AIDQOTfN"
json_df = GET(paste0("https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=", my_api_keys))
json_df
## Response [https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=UzqBA5xShuTAZ3Sg9AuACli4AIDQOTfN]
##   Date: 2022-10-28 23:34
##   Status: 200
##   Content-Type: application/json; charset=UTF-8
##   Size: 12.5 kB
nybook <- fromJSON(rawToChar(json_df$content))
names(nybook) 
## [1] "status"      "copyright"   "num_results" "results"
nybook$results
nybks_flat <- flatten(nybook$results)
str(nybks_flat)
## 'data.frame':    59 obs. of  6 variables:
##  $ list_name            : chr  "Combined Print and E-Book Fiction" "Combined Print and E-Book Nonfiction" "Hardcover Fiction" "Hardcover Nonfiction" ...
##  $ display_name         : chr  "Combined Print & E-Book Fiction" "Combined Print & E-Book Nonfiction" "Hardcover Fiction" "Hardcover Nonfiction" ...
##  $ list_name_encoded    : chr  "combined-print-and-e-book-fiction" "combined-print-and-e-book-nonfiction" "hardcover-fiction" "hardcover-nonfiction" ...
##  $ oldest_published_date: chr  "2011-02-13" "2011-02-13" "2008-06-08" "2008-06-08" ...
##  $ newest_published_date: chr  "2022-11-06" "2022-11-06" "2022-11-06" "2022-11-06" ...
##  $ updated              : chr  "WEEKLY" "WEEKLY" "WEEKLY" "WEEKLY" ...
nybooks_tbl <- as.data.frame(nybks_flat)
nybooks_tbl