HAZAL GUNDUZ
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.
Step-1: Loading the required packages
library(httr)
library(jsonlite)
library(knitr)
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
We have already requested the API key to get / read the data from the NY Times API. We are using the books_api from the NY Times page : http://developer.nytimes.com/ and https://developer.nytimes.com/docs/books-product/1/routes/lists/best-sellers/history.json/get
nyt_books_url <- ("https://developer.nytimes.com/docs/books-product/1/routes/lists/best-sellers/history.json/get")
("https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json")
## [1] "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json"
nyt_books <- GET(nyt_books_url, query = list(api_key = "a01880c89030475c908b46f8072e2a02"))
nyt_books$status_code
## [1] 200
api_key <- "a01880c89030475c908b46f8072e2a02"
path <- ("https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json")
r <- GET(path, query = list(api_key = "a01880c89030475c908b46f8072e2a02"))
response <- content(r, as = "text", encoding = "UTF-8")
text_content <- content(nyt_books, as = "text", encoding = "UTF-8")
json_content_df <- text_content
df <- fromJSON(response) %>%
data.frame()
nrow(df)
## [1] 1
Github => https://github.com/Gunduzhazal/web-apis
Rpubs => https://rpubs.com/gunduzhazal/825879