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.

The API that I decide to use is called “Movie Reviews API” where you can search New York Times movie reviews by keyword and opening date and filter by Critics’ Picks.
As a result of our request, we can get only 20 reviews per request.
I will look for reviews about the films that contain the word “Lord”.
To access the NYT URL, we will need our api key and the URL with the request in it. Once we get the information from the URL, we can check if the request was successful by using status_code, if we get code 200, we got an array of movie critics.

key_api <- "YFbXOzrWx1MdeAh7CzfjJU7iSMGcYE4m"
url <- paste("https://api.nytimes.com/svc/movies/v2/reviews/search.json?query=lord&api-key=", key_api)
reviews <- GET(url)
reviews$status_code
## [1] 200

We can check what we have inside of our responce.

summary(reviews)
##             Length Class       Mode       
## url             1  -none-      character  
## status_code     1  -none-      numeric    
## headers        21  insensitive list       
## all_headers     1  -none-      list       
## cookies         7  data.frame  list       
## content     14781  -none-      raw        
## date            1  POSIXct     numeric    
## times           6  -none-      numeric    
## request         7  request     list       
## handle          1  curl_handle externalptr

Since the response contains different types of data and we want to read this json file it has to be in the text format.
Once we read json file, we will transform it to data frame. To read json file we use function fromJSON() with the parameter flatten=TRUE to flatten nested data frames.

reviews <- content(reviews, as = "text")
df_reviews <- fromJSON(reviews, flatten = TRUE)
df_reviews <- data.frame(df_reviews$results, stringsAsFactors = FALSE)

We got a data frame with 20 reviews and 16 columns.

colnames(df_reviews)
##  [1] "display_title"            "mpaa_rating"             
##  [3] "critics_pick"             "byline"                  
##  [5] "headline"                 "summary_short"           
##  [7] "publication_date"         "opening_date"            
##  [9] "date_updated"             "link.type"               
## [11] "link.url"                 "link.suggested_link_text"
## [13] "multimedia.type"          "multimedia.src"          
## [15] "multimedia.height"        "multimedia.width"
head(df_reviews)
##          display_title mpaa_rating critics_pick            byline
## 1 The Book of Delights                        0        Teo Bugbee
## 2            Claydream                        0    Ben Kenigsberg
## 3          Metal Lords           R            0 Natalia Winkelman
## 4  The Kissing Booth 2       TV-14            0 Natalia Winkelman
## 5       Lords of Chaos           R            0    MANOHLA DARGIS
## 6             Overlord           R            0       BILGE EBIRI
##                                                           headline
## 1                   ‘The Book of Delights’ Review: A Heady Romance
## 2                              ‘Claydream’ Review: Same Mold Story
## 3                         ‘Metal Lords’ Review: Shred of the Class
## 4  ‘The Kissing Booth 2’ Review: Another Dose of Prep School Drama
## 5  ‘Lords of Chaos’ Review: Where Anomie and Speed Metal Meet Doom
## 6 ‘Overlord’ Review: Old-Fashioned Nazi-Killing, With a Gory Twist
##                                                                                                                                             summary_short
## 1 A schoolteacher’s odyssey into erotic self-discovery and existential desire fuels this Brazilian drama, which is based on a novel by Clarice Lispector.
## 2                         In this documentary, Will Vinton, the animation visionary who gave life to the California Raisins and the Noid, gets a tribute.
## 3                                                          Teenage boys battling angst and bullies form a heavy metal band in this genuine Netflix movie.
## 4                A high school senior juggles college applications and a slippery, long-distance beau in this Netflix sequel to the popular teen rom-com.
## 5       Inspired by a sordid, true story, this English-language movie tracks a group of Norwegian death-metal musicians whose lives go murderously wrong.
## 6                                The director Julius Avery delivers a fairly predictable action-horror hybrid about American soldiers behind enemy lines.
##   publication_date opening_date        date_updated link.type
## 1       2022-09-01         <NA> 2022-09-01 16:13:03   article
## 2       2022-08-04         <NA> 2022-08-04 11:02:09   article
## 3       2022-04-07   2022-04-08 2022-04-07 11:05:02   article
## 4       2020-07-24   2020-07-24 2020-07-24 07:06:02   article
## 5       2019-02-06         <NA> 2019-03-06 17:44:02   article
## 6       2018-11-08   2018-11-09 2018-12-08 17:44:02   article
##                                                                     link.url
## 1 https://www.nytimes.com/2022/09/01/movies/the-book-of-delights-review.html
## 2            https://www.nytimes.com/2022/08/04/movies/claydream-review.html
## 3          https://www.nytimes.com/2022/04/07/movies/metal-lords-review.html
## 4  https://www.nytimes.com/2020/07/24/movies/the-kissing-booth-2-review.html
## 5       https://www.nytimes.com/2019/02/06/movies/lords-of-chaos-review.html
## 6             https://www.nytimes.com/2018/11/08/movies/overlord-review.html
##                                 link.suggested_link_text     multimedia.type
## 1 Read the New York Times Review of The Book of Delights mediumThreeByTwo210
## 2            Read the New York Times Review of Claydream mediumThreeByTwo210
## 3          Read the New York Times Review of Metal Lords mediumThreeByTwo210
## 4  Read the New York Times Review of The Kissing Booth 2 mediumThreeByTwo210
## 5       Read the New York Times Review of Lords of Chaos mediumThreeByTwo210
## 6             Read the New York Times Review of Overlord mediumThreeByTwo210
##                                                                                                                                    multimedia.src
## 1                                   https://static01.nyt.com/images/2022/09/01/arts/01book-of-delights/01book-of-delights-mediumThreeByTwo440.jpg
## 2                                                     https://static01.nyt.com/images/2022/08/03/arts/claydream/claydream-mediumThreeByTwo440.jpg
## 3 https://static01.nyt.com/images/2022/04/07/arts/07metal-lords-art/merlin_204842160_43efd1f2-b74f-467f-a87b-0df43247fd0d-mediumThreeByTwo440.jpg
## 4                                                       https://static01.nyt.com/images/2020/07/24/arts/kissing1/kissing1-mediumThreeByTwo210.jpg
## 5                                        https://static01.nyt.com/images/2019/02/08/arts/08lordsofchaos/08lordsofchaos-mediumThreeByTwo210-v3.jpg
## 6                                                   https://static01.nyt.com/images/2018/11/09/arts/09overlord/09overlord-mediumThreeByTwo210.jpg
##   multimedia.height multimedia.width
## 1               140              210
## 2               140              210
## 3               140              210
## 4               140              210
## 5               140              210
## 6               140              210

We need to rename the columns, so it will be easier to work with the data frame during analysis.

colnames(df_reviews) <- c("Title", "MPAA_rating", "Critics_pick", "Review_author", "Headline", "Summary_short", "Publication_date", "Movie_opening_date",          "Date_updated", "link_type", "url", "suggested_link_text", "multimedia_type",  "multimedia_src", "multimedia_height", "multimedia_width")

Some of the columns contain information we don’t need. For example, “link_type”, info about multimedia image.

df_reviews <- df_reviews[, -c(10,13,14,15,16)]
head(df_reviews)
##                  Title MPAA_rating Critics_pick     Review_author
## 1 The Book of Delights                        0        Teo Bugbee
## 2            Claydream                        0    Ben Kenigsberg
## 3          Metal Lords           R            0 Natalia Winkelman
## 4  The Kissing Booth 2       TV-14            0 Natalia Winkelman
## 5       Lords of Chaos           R            0    MANOHLA DARGIS
## 6             Overlord           R            0       BILGE EBIRI
##                                                           Headline
## 1                   ‘The Book of Delights’ Review: A Heady Romance
## 2                              ‘Claydream’ Review: Same Mold Story
## 3                         ‘Metal Lords’ Review: Shred of the Class
## 4  ‘The Kissing Booth 2’ Review: Another Dose of Prep School Drama
## 5  ‘Lords of Chaos’ Review: Where Anomie and Speed Metal Meet Doom
## 6 ‘Overlord’ Review: Old-Fashioned Nazi-Killing, With a Gory Twist
##                                                                                                                                             Summary_short
## 1 A schoolteacher’s odyssey into erotic self-discovery and existential desire fuels this Brazilian drama, which is based on a novel by Clarice Lispector.
## 2                         In this documentary, Will Vinton, the animation visionary who gave life to the California Raisins and the Noid, gets a tribute.
## 3                                                          Teenage boys battling angst and bullies form a heavy metal band in this genuine Netflix movie.
## 4                A high school senior juggles college applications and a slippery, long-distance beau in this Netflix sequel to the popular teen rom-com.
## 5       Inspired by a sordid, true story, this English-language movie tracks a group of Norwegian death-metal musicians whose lives go murderously wrong.
## 6                                The director Julius Avery delivers a fairly predictable action-horror hybrid about American soldiers behind enemy lines.
##   Publication_date Movie_opening_date        Date_updated
## 1       2022-09-01               <NA> 2022-09-01 16:13:03
## 2       2022-08-04               <NA> 2022-08-04 11:02:09
## 3       2022-04-07         2022-04-08 2022-04-07 11:05:02
## 4       2020-07-24         2020-07-24 2020-07-24 07:06:02
## 5       2019-02-06               <NA> 2019-03-06 17:44:02
## 6       2018-11-08         2018-11-09 2018-12-08 17:44:02
##                                                                          url
## 1 https://www.nytimes.com/2022/09/01/movies/the-book-of-delights-review.html
## 2            https://www.nytimes.com/2022/08/04/movies/claydream-review.html
## 3          https://www.nytimes.com/2022/04/07/movies/metal-lords-review.html
## 4  https://www.nytimes.com/2020/07/24/movies/the-kissing-booth-2-review.html
## 5       https://www.nytimes.com/2019/02/06/movies/lords-of-chaos-review.html
## 6             https://www.nytimes.com/2018/11/08/movies/overlord-review.html
##                                      suggested_link_text
## 1 Read the New York Times Review of The Book of Delights
## 2            Read the New York Times Review of Claydream
## 3          Read the New York Times Review of Metal Lords
## 4  Read the New York Times Review of The Kissing Booth 2
## 5       Read the New York Times Review of Lords of Chaos
## 6             Read the New York Times Review of Overlord