library(jsonlite)
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.4.4
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
library(httr)
## Warning: package 'httr' was built under R version 3.4.4
library(RCurl)
## Loading required package: bitops
## 
## Attaching package: 'RCurl'
## The following object is masked from 'package:tidyr':
## 
##     complete

This paste command adds the api key received from the NY Times dev website to the uRL making it possible to run our query:

newurl <- "https://api.nytimes.com/svc/archive/v1/1872/4.json"
apikey <- "?api-key=2e857cb77ddc49d6b55873a1a0236030"
newurl <- paste(newurl, apikey, sep="")

Adding this query at the end as well to narrow our results:

ny <- "&ny=NY"
newurl <- paste(newurl, ny, sep="")

The GET command pulls data from the datasource. Once we have the information we have to convert the response to text format, so the ‘fromJSON’ command can read it. Once we have it converted to an acceptable JSON format, we look at the ‘docs’ aspect of the data as that is where the pertinent data lives.

resp <- GET(newurl, accept_json())
json <- fromJSON(content(resp, as="text"))
## No encoding supplied: defaulting to UTF-8.
thedf <- data.frame(json$response$docs)

Now we place in the data frame

head(thedf)