knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(httr)
library(jsonlite)
##
## Attaching package: 'jsonlite'
##
## The following object is masked from 'package:purrr':
##
## flatten
key = rstudioapi::askForPassword('Please enter your password:')
best_sellers <- httr::GET(paste0('https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json?api-key=',key))
book_text = content(best_sellers,as='text')
books_data <- jsonlite::fromJSON(book_text)
books_df <- books_data$results
unnested_books <- books_df |> tidyr::unnest(cols=c(isbns,reviews),keep_empty = TRUE)
head(unnested_books)
## # A tibble: 6 × 15
## title descr…¹ contr…² author contr…³ price age_g…⁴ publi…⁵ isbn10 isbn13
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 "\"I GIVE … The au… by Dia… Diana… "" 0.00 "" Dell 03991… 97803…
## 2 "\"MOST BL… A char… by Ann… Annet… "" 0.00 "" Liveri… 08714… 97808…
## 3 "#ASKGARYV… The en… by Gar… Gary … "" 0.00 "" Harper… 00622… 97800…
## 4 "#ASKGARYV… The en… by Gar… Gary … "" 0.00 "" Harper… 00622… 97800…
## 5 "#GIRLBOSS" An onl… by Sop… Sophi… "" 0.00 "" Portfo… 03991… 97803…
## 6 "#GIRLBOSS" An onl… by Sop… Sophi… "" 0.00 "" Portfo… 15918… 97815…
## # … with 5 more variables: ranks_history <list>, book_review_link <chr>,
## # first_chapter_link <chr>, sunday_review_link <chr>,
## # article_chapter_link <chr>, and abbreviated variable names ¹description,
## # ²contributor, ³contributor_note, ⁴age_group, ⁵publisher
books_df |> tidyr::unnest(cols=c(ranks_history),keep_empty = TRUE) |>
dplyr::arrange(rank)
## # A tibble: 30 × 21
## title descr…¹ contr…² author contr…³ price age_g…⁴ publi…⁵ isbns prima…⁶
## <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <lis> <chr>
## 1 "(RE)BORN… "The s… by Rog… Roger… "" 0.00 "" Dey St… <df> 006295…
## 2 "(RE)BORN… "The s… by Rog… Roger… "" 0.00 "" Dey St… <df> 006295…
## 3 "#ASKGARY… "The e… by Gar… Gary … "" 0.00 "" Harper… <df> 006227…
## 4 "#ASKGARY… "The e… by Gar… Gary … "" 0.00 "" Harper… <df> 006227…
## 5 "'TIS THE… "Two c… by Rob… Ron C… "" 0.00 "" Harleq… <df> 077831…
## 6 "\"I GIVE… "The a… by Dia… Diana… "" 0.00 "" Dell <df> 039917…
## 7 "#GIRLBOS… "An on… by Sop… Sophi… "" 0.00 "" Portfo… <df> 159184…
## 8 "#GIRLBOS… "An on… by Sop… Sophi… "" 0.00 "" Portfo… <df> 159184…
## 9 "#GIRLBOS… "An on… by Sop… Sophi… "" 0.00 "" Portfo… <df> 039916…
## 10 "#GIRLBOS… "An on… by Sop… Sophi… "" 0.00 "" Portfo… <df> 159184…
## # … with 20 more rows, 11 more variables: primary_isbn13 <chr>, rank <int>,
## # list_name <chr>, display_name <chr>, published_date <chr>,
## # bestsellers_date <chr>, weeks_on_list <int>, rank_last_week <int>,
## # asterisk <int>, dagger <int>, reviews <list>, and abbreviated variable
## # names ¹description, ²contributor, ³contributor_note, ⁴age_group,
## # ⁵publisher, ⁶primary_isbn10
Overall, it is fairly easy to load JSON data from an API into a dataframe to prepare for further analysis with minimal lines of codes needed.