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.
For this assignment we will be using API to Get movie reviews that are critics’ picks:
/reviews/picks.json
Create a developer account on the developer.nytimes.com and register an application and sign up for API Key
Load the required libraries
library('jsonlite')
library('httr')
library('tidyverse')
Read the API key provided by New York Time while registering the app
pwfile <- "apiKey.txt"
if ( file.exists(pwfile) )
{
apiKeyValue <- readLines(pwfile)
}else
{
apiKeyValue <- rstudioapi::askForPassword( prompt = "Please enter API key:" )
}
Append the API key to the base url
nytmoviePickBaseUrl <- 'https://api.nytimes.com/svc/movies/v2/reviews/picks.json'
finalUrl <- paste0(nytmoviePickBaseUrl,"?api-key=", apiKeyValue)
moviePicksJson <-read_json(finalUrl, simplifyVector = TRUE)
moviePicksJson$status
## [1] "OK"
#Check the http status code
if(moviePicksJson$status =='OK'){
#Select first 8 columns and create a new data frame
moviePicksJsonDf <- moviePicksJson$results %>% select(1:8)
DT::datatable(moviePicksJsonDf)
}
Successfully retrieved the top movie reviews that are critics picks by using the developer API Converted the json data into data frame