## load libraries
library(jsonlite)
library(tidyr)
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
## read in NYT Book Review JSON data
book_review_data <- fromJSON("https://api.nytimes.com/svc/topstories/v2/books%2Freview.json?api-key=27YsDTxETOpdu3yfsfwZAGCRuLofUvvb")

##convert to data frame
book_review_df <- as.data.frame(book_review_data)

##get overview of data frame
glimpse(book_review_df)
## Rows: 34
## Columns: 24
## $ status                      <chr> "OK", "OK", "OK", "OK", "OK", "OK", "OK", …
## $ copyright                   <chr> "Copyright (c) 2023 The New York Times Com…
## $ section                     <chr> "Book Review", "Book Review", "Book Review…
## $ last_updated                <chr> "2023-11-05T07:03:32-05:00", "2023-11-05T0…
## $ num_results                 <int> 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34…
## $ results.section             <chr> "books", "books", "books", "books", "books…
## $ results.subsection          <chr> "review", "review", "review", "", "review"…
## $ results.title               <chr> "The Hard-Partying College Kids Who Were A…
## $ results.abstract            <chr> "Max Marshall’s “Among the Bros” investiga…
## $ results.url                 <chr> "https://www.nytimes.com/2023/11/04/books/…
## $ results.uri                 <chr> "nyt://article/baeb03ab-028a-5e2b-9855-0ab…
## $ results.byline              <chr> "By Rachel Fleit", "By Andrew Meier", "By …
## $ results.item_type           <chr> "Article", "Article", "Article", "Article"…
## $ results.updated_date        <chr> "2023-11-05T09:53:06-05:00", "2023-11-05T0…
## $ results.created_date        <chr> "2023-11-04T05:00:26-04:00", "2023-11-05T0…
## $ results.published_date      <chr> "2023-11-04T05:00:26-04:00", "2023-11-05T0…
## $ results.material_type_facet <chr> "", "", "", "", "", "", "", "", "", "", ""…
## $ results.kicker              <chr> "Nonfiction", "Nonfiction", "nonfiction", …
## $ results.des_facet           <list> <"Books and Literature", "Among the Bros:…
## $ results.org_facet           <list> "College of Charleston", <>, <>, <>, <>, …
## $ results.per_facet           <list> "Marshall, Max (Journalist)", <"Longo, Ma…
## $ results.geo_facet           <list> <>, <"USSR (Former Soviet Union)", "Easte…
## $ results.multimedia          <list> [<data.frame[3 x 8]>], [<data.frame[3 x 8…
## $ results.short_url           <chr> "", "", "", "", "", "", "", "", "https://n…
## unnest columns that are lists and lists of data frames
book_review_df <- book_review_df %>%
  unnest(results.des_facet) %>%
  unnest(results.org_facet) %>%
  unnest(results.per_facet) %>%
  unnest(results.geo_facet) %>%
  unnest(results.multimedia, names_sep = ".")
## get overview of final data frame
glimpse(book_review_df)
## Rows: 243
## Columns: 31
## $ status                       <chr> "OK", "OK", "OK", "OK", "OK", "OK", "OK",…
## $ copyright                    <chr> "Copyright (c) 2023 The New York Times Co…
## $ section                      <chr> "Book Review", "Book Review", "Book Revie…
## $ last_updated                 <chr> "2023-11-05T07:03:32-05:00", "2023-11-05T…
## $ num_results                  <int> 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 3…
## $ results.section              <chr> "books", "books", "books", "books", "book…
## $ results.subsection           <chr> "", "", "", "", "", "", "", "", "", "", "…
## $ results.title                <chr> "Read Your Way Through Lima", "Read Your …
## $ results.abstract             <chr> "Lima is a city of contrasts and contradi…
## $ results.url                  <chr> "https://www.nytimes.com/2023/11/01/books…
## $ results.uri                  <chr> "nyt://article/db1cb62a-d931-5066-92ec-25…
## $ results.byline               <chr> "By Augusto Higa Oshiro and translated by…
## $ results.item_type            <chr> "Article", "Article", "Article", "Article…
## $ results.updated_date         <chr> "2023-11-01T09:56:53-04:00", "2023-11-01T…
## $ results.created_date         <chr> "2023-11-01T05:00:12-04:00", "2023-11-01T…
## $ results.published_date       <chr> "2023-11-01T05:00:12-04:00", "2023-11-01T…
## $ results.material_type_facet  <chr> "", "", "", "", "", "", "", "", "", "", "…
## $ results.kicker               <chr> "", "", "", "", "", "", "", "", "", "", "…
## $ results.des_facet            <chr> "Books and Literature", "Books and Litera…
## $ results.org_facet            <chr> "Shining Path", "Shining Path", "Shining …
## $ results.per_facet            <chr> "Higa Oshiro, Augusto (1946-2023)", "Higa…
## $ results.geo_facet            <chr> "Lima (Peru)", "Lima (Peru)", "Lima (Peru…
## $ results.multimedia.url       <chr> "https://static01.nyt.com/images/2023/11/…
## $ results.multimedia.format    <chr> "Super Jumbo", "threeByTwoSmallAt2X", "La…
## $ results.multimedia.height    <int> 1367, 400, 150, 1367, 400, 150, 1367, 400…
## $ results.multimedia.width     <int> 2048, 600, 150, 2048, 600, 150, 2048, 600…
## $ results.multimedia.type      <chr> "image", "image", "image", "image", "imag…
## $ results.multimedia.subtype   <chr> "photo", "photo", "photo", "photo", "phot…
## $ results.multimedia.caption   <chr> "", "", "", "", "", "", "", "", "", "", "…
## $ results.multimedia.copyright <chr> "Raphaelle Macaron", "Raphaelle Macaron",…
## $ results.short_url            <chr> "", "", "", "", "", "", "", "", "", "", "…