Introduction:

The assignment this week we are supposed to choose one of the New York Times APIs,construct an interface in R to read in the JSON data, and to transform it into an R DataFrame.

Importing the Libraries

Here I have imported the httr library to read the url,the tidyverse so I can use dyplr to use the pipes and jsonlite to read the json data.

library(httr)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 4.1.3
## 
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
## 
##     flatten

Reading the APIs

The API I had chosen was the New York Times movie reviews API,it had contained many information on nyt critics that reviewed various kinds of movies. I had to tweak the link a few times to make sure the connection was working.Checking the status_code I got 200 which means the connection was successful.

##reading the url from the API I had to tweak it a few times and even then I didn't understand how to hide the api key from the users 

r <- GET("https://api.nytimes.com/svc/movies/v2/reviews/picks.json?api-key=cmTGJ9nWOxo50ATlttCSZaOaXI3CNYEe")

r$status_code
## [1] 200

The raw Bytes:

I used r$content to take a glance at the raw bytes inside the url and then I extract the content of the url using the content function using the text argument.

head(r$content)
## [1] 7b 22 73 74 61 74
r_data <- content(r, as = "text")

Converting to a DataFrame

From here I just simply used the dplyr function and I piped the r_data into the Json data and then I converted it into a data_frame which I displayed below.

df <- r_data %>%
  fromJSON() %>%
  as.data.frame()

Cleaning Out Unnecessary columns

clean_df <-df %>%
  select(-c(1:4,7,14:15))
head(clean_df)
##               results.display_title results.mpaa_rating      results.byline
## 1 Everything Everywhere All at Once                   R          A.O. Scott
## 2             You Are Not My Mother                     Jeannette Catsoulis
## 3                    Wood and Water                         Beatrice Loayza
## 4                          Superior                           Amy Nicholson
## 5                                                               Glenn Kenny
## 6                        Întregalde                          Manohla Dargis
##                                                       results.headline
## 1 ‘Everything Everywhere All at Once’ Review: It’s Messy, and Glorious
## 2                 ‘You Are Not My Mother’ Review: Parental Misguidance
## 3                    ‘Wood and Water’ Review: The Distances Between Us
## 4                                    ‘Superior’ Review: Double Fantasy
## 5       ‘The Torch’ Review: The Blues Legend Buddy Guy Sows His Legacy
## 6                                    ‘Intregalde’ Review: Oh, Be Good!
##                                                                                                                                                                                results.summary_short
## 1                                                                                                 Michelle Yeoh stars as a stressed-out laundromat owner dragged into cosmic battle and genre chaos.
## 2                                                                                                A lonely teenager is traumatized by her mother’s volatile behavior in this impressive horror debut.
## 3 In this elegant feature debut about modern alienation, the German writer-director Jonas Bak casts his real-life mother as a retired secretary who travels to Hong Kong to visit her estranged son.
## 4                                                                                Two identical sisters reunite under mysterious circumstances in a compelling debut feature from Erin Vassilopoulos.
## 5                                                                                                                       A new documentary by Jim Farrell focuses on the musician’s sense of mission.
## 6                                                                 A Romanian satire charts what happens when some humanitarian aid workers set out to save others (and need to be saved themselves).
##   results.publication_date results.opening_date results.date_updated
## 1               2022-03-24           2022-03-25  2022-03-24 16:58:03
## 2               2022-03-24                 <NA>  2022-03-24 15:32:03
## 3               2022-03-24                 <NA>  2022-03-24 14:09:03
## 4               2022-03-24                 <NA>  2022-03-24 11:01:03
## 5               2022-03-17                 <NA>  0001-01-01 00:00:00
## 6               2022-03-17           2022-03-18  2022-03-17 15:59:03

Conclusion:

I had some difficulties understanding the task mostly this is my first time exposed to APIs and reading data from the web.But overall I had fun :).Looking through the data I see there are a bunch of reviews for movies I have never heard of.. There were also a lot of unecessary columns in which I had removed to make it easier for the reader to understand.