This assignment consists of choosing one of the New York Times APIs , construct an interface in R to read in the JSON data, and transform it into an R Data Frame.
To complete the assignment, i used the Top Stories API of the New York Times. This API returns the latest top stories from a specific New York Times section.I will select the science section while defining my API Key and endpoint.
library(httr)
library(jsonlite)
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
# Define API key and endpoint
api_key <- "NJHc74npPhrzMP2MXwh7UKuAJOI367VG"
section <- "science"
url <- paste0(
"https://api.nytimes.com/svc/topstories/v2/",
section,
".json?api-key=", api_key
)
url
## [1] "https://api.nytimes.com/svc/topstories/v2/science.json?api-key=NJHc74npPhrzMP2MXwh7UKuAJOI367VG"
# Send GET request
response <- GET(url)
# Parse JSON content
data_json <- content(response, as = "text")
## No encoding supplied: defaulting to UTF-8.
data_list <- fromJSON(data_json, flatten = TRUE)
It is important to note that articles are kept under the “results” key of the JSON response.
# Extract the 'results' list and convert it into a data frame
articles_df <- as.data.frame(data_list$results)
head(articles_df,10)