Introduction

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.

I started by creating an account using the above link. Next, I set up a key and familiarized myself with the API documentation. Once I was able to access the API through R, I separated the data, transformed, then combined it back together.

##Libraries

Accessing API

url <- 'https://api.nytimes.com/svc/movies/v2/reviews/all.json'
key <- 'GAC1iHZlrjry6V65NHG3DGtCkCOCiikh'

movie_review_data <- GET(url, query = list('api-key' = key))
movie_review_content <- content(movie_review_data, 'text')

movie_review_content <- fromJSON(movie_review_content)

# names of the arrays from the API call
(c_names <- names(movie_review_content))
## [1] "status"      "copyright"   "has_more"    "num_results" "results"

Viewing the Data Stored

We see that within the JSON file, there are five arrays of data. Let’s see what is in each one of the arrays and transform the data that we want into a data frame

df1 <- movie_review_content[c_names[1]]
str(df1)
## List of 1
##  $ status: chr "OK"
df2 <- movie_review_content[c_names[2]]
str(df2)
## List of 1
##  $ copyright: chr "Copyright (c) 2020 The New York Times Company. All Rights Reserved."
df3 <- movie_review_content[c_names[3]]
str(df3)
## List of 1
##  $ has_more: logi TRUE
df4 <- movie_review_content[c_names[4]]
str(df4)
## List of 1
##  $ num_results: int 20
df5 <- movie_review_content[c_names[5]]
str(df5)
## List of 1
##  $ results:'data.frame': 20 obs. of  11 variables:
##   ..$ display_title   : chr [1:20] "What the Constitution Means to Me" "Synchronic" "Ham on Rye" "The Witches" ...
##   ..$ mpaa_rating     : chr [1:20] "" "R" "" "PG" ...
##   ..$ critics_pick    : int [1:20] 1 1 1 0 0 0 0 0 0 0 ...
##   ..$ byline          : chr [1:20] "Elisabeth Vincentelli" "Glenn Kenny" "Glenn Kenny" "Manohla Dargis" ...
##   ..$ headline        : chr [1:20] "‘What the Constitution Means to Me’ Review: Pursuits of Happiness" "‘Synchronic’ Review: Twisted, Trippy Trips Through Time" "‘Ham on Rye’ Review: Coming of Age, With Existential Unease" "‘The Witches’ Review: A Tale of Mice and Women, Toil and Trouble" ...
##   ..$ summary_short   : chr [1:20] "The timing could not be better for Heidi Schreck’s affecting play about the Constitution’s impact on our daily "| __truncated__ "The filmmakers Justin Benson and Aaron Moorhead bend reality in this drug-fueled sci-fi horror." "Tyler Taormina’s debut film starts in the vein of classic high-school comedies until it turns toward a dark surrealism." "In this new take on the Roald Dahl book, Robert Zemeckis sets loose his cameras, and Octavia Spencer side-eyes Anne Hathaway." ...
##   ..$ publication_date: chr [1:20] "2020-10-22" "2020-10-22" "2020-10-22" "2020-10-22" ...
##   ..$ opening_date    : chr [1:20] "2020-10-16" "2020-10-23" NA "2020-10-22" ...
##   ..$ date_updated    : chr [1:20] "2020-10-22 17:12:02" "2020-10-24 02:44:19" "2020-10-22 11:04:07" "2020-10-22 16:48:02" ...
##   ..$ link            :'data.frame': 20 obs. of  3 variables:
##   .. ..$ type               : chr [1:20] "article" "article" "article" "article" ...
##   .. ..$ url                : chr [1:20] "http://www.nytimes.com/2020/10/22/movies/what-the-constitution-means-to-me-review.html" "http://www.nytimes.com/2020/10/22/movies/synchronic-review.html" "http://www.nytimes.com/2020/10/22/movies/ham-on-rye-review.html" "http://www.nytimes.com/2020/10/22/movies/the-witches-review.html" ...
##   .. ..$ suggested_link_text: chr [1:20] "Read the New York Times Review of What the Constitution Means to Me" "Read the New York Times Review of Synchronic" "Read the New York Times Review of Ham on Rye" "Read the New York Times Review of The Witches" ...
##   ..$ multimedia      :'data.frame': 20 obs. of  4 variables:
##   .. ..$ type  : chr [1:20] "mediumThreeByTwo210" "mediumThreeByTwo210" "mediumThreeByTwo210" "mediumThreeByTwo210" ...
##   .. ..$ src   : chr [1:20] "https://static01.nyt.com/images/2020/10/22/arts/constitution1/merlin_178810461_91635d97-eb31-4e4d-8ad9-57e22ad3"| __truncated__ "https://static01.nyt.com/images/2020/10/22/arts/22synchronic-art/22synchronic-art-mediumThreeByTwo210.jpg" "https://static01.nyt.com/images/2020/10/23/arts/22hamonrye/22hamonrye-mediumThreeByTwo210.jpg" "https://static01.nyt.com/images/2020/10/23/arts/00the-witches/00the-witches-mediumThreeByTwo210-v3.jpg" ...
##   .. ..$ width : int [1:20] 210 210 210 210 210 210 210 210 210 210 ...
##   .. ..$ height: int [1:20] 140 140 140 140 140 140 140 140 140 140 ...

Transforming Dataframes

The variable df5 is the winner, however, “link” and “multimedia” are saved as data frames. We need to separate these data frames, transform them, then bind them together.

# we see that the first nine column names have the prefix of "results."
results_df <- as.data.frame(df5)
results_df <- results_df[1:9]

# transforming column names
c_names <- colnames(results_df) %>%
  str_replace('results.', '')

#  replace the column names
(colnames(results_df) <- c_names)
## [1] "display_title"    "mpaa_rating"      "critics_pick"     "byline"          
## [5] "headline"         "summary_short"    "publication_date" "opening_date"    
## [9] "date_updated"
link_df <- df5$results$link %>%
  as.data.frame()

multimedia_df <- df5$results$multimedia %>%
  as.data.frame()

Combining the Data into a Data Frame

Finally, we can combine our results together into one data frame.

movie_df <- cbind(results_df, link_df, multimedia_df)
str(movie_df)
## 'data.frame':    20 obs. of  16 variables:
##  $ display_title      : chr  "What the Constitution Means to Me" "Synchronic" "Ham on Rye" "The Witches" ...
##  $ mpaa_rating        : chr  "" "R" "" "PG" ...
##  $ critics_pick       : int  1 1 1 0 0 0 0 0 0 0 ...
##  $ byline             : chr  "Elisabeth Vincentelli" "Glenn Kenny" "Glenn Kenny" "Manohla Dargis" ...
##  $ headline           : chr  "‘What the Constitution Means to Me’ Review: Pursuits of Happiness" "‘Synchronic’ Review: Twisted, Trippy Trips Through Time" "‘Ham on Rye’ Review: Coming of Age, With Existential Unease" "‘The Witches’ Review: A Tale of Mice and Women, Toil and Trouble" ...
##  $ summary_short      : chr  "The timing could not be better for Heidi Schreck’s affecting play about the Constitution’s impact on our daily "| __truncated__ "The filmmakers Justin Benson and Aaron Moorhead bend reality in this drug-fueled sci-fi horror." "Tyler Taormina’s debut film starts in the vein of classic high-school comedies until it turns toward a dark surrealism." "In this new take on the Roald Dahl book, Robert Zemeckis sets loose his cameras, and Octavia Spencer side-eyes Anne Hathaway." ...
##  $ publication_date   : chr  "2020-10-22" "2020-10-22" "2020-10-22" "2020-10-22" ...
##  $ opening_date       : chr  "2020-10-16" "2020-10-23" NA "2020-10-22" ...
##  $ date_updated       : chr  "2020-10-22 17:12:02" "2020-10-24 02:44:19" "2020-10-22 11:04:07" "2020-10-22 16:48:02" ...
##  $ type               : chr  "article" "article" "article" "article" ...
##  $ url                : chr  "http://www.nytimes.com/2020/10/22/movies/what-the-constitution-means-to-me-review.html" "http://www.nytimes.com/2020/10/22/movies/synchronic-review.html" "http://www.nytimes.com/2020/10/22/movies/ham-on-rye-review.html" "http://www.nytimes.com/2020/10/22/movies/the-witches-review.html" ...
##  $ suggested_link_text: chr  "Read the New York Times Review of What the Constitution Means to Me" "Read the New York Times Review of Synchronic" "Read the New York Times Review of Ham on Rye" "Read the New York Times Review of The Witches" ...
##  $ type               : chr  "mediumThreeByTwo210" "mediumThreeByTwo210" "mediumThreeByTwo210" "mediumThreeByTwo210" ...
##  $ src                : chr  "https://static01.nyt.com/images/2020/10/22/arts/constitution1/merlin_178810461_91635d97-eb31-4e4d-8ad9-57e22ad3"| __truncated__ "https://static01.nyt.com/images/2020/10/22/arts/22synchronic-art/22synchronic-art-mediumThreeByTwo210.jpg" "https://static01.nyt.com/images/2020/10/23/arts/22hamonrye/22hamonrye-mediumThreeByTwo210.jpg" "https://static01.nyt.com/images/2020/10/23/arts/00the-witches/00the-witches-mediumThreeByTwo210-v3.jpg" ...
##  $ width              : int  210 210 210 210 210 210 210 210 210 210 ...
##  $ height             : int  140 140 140 140 140 140 140 140 140 140 ...

Conclusion

We see that the data stored in the link and mulitmedia data frames are redudant and not needed. The information found in the results data frame holds information that is easier to read and understand.