Web APIs

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.

Library

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

Retrieve book data from book API

Define the search date as 2019-01-01 and use fromJSON to generate call.

key <- "&api-key=76363c9e70bc401bac1e6ad88b13bd1d"
url <- "http://api.nytimes.com/svc/books/v2/lists/overview.json?published_date=2019-01-01"
req <- fromJSON(paste0(url, key))

Output

Choose the two list of the bestsellers, combine and show the information of author, title, publisher and weeks_on_list.

bestsellers <- req$results$list
category1 <- bestsellers[[1, "books"]]
category2 <- bestsellers[[2, "books"]]
category<-rbind(category1,category2)

subset(category, select = c("author", "title", "publisher","weeks_on_list"))
##                             author                   title     publisher
## 1                     John Grisham           THE RECKONING     Doubleday
## 2                  Nicholas Sparks            EVERY BREATH Grand Central
## 3                 George RR Martin          FIRE AND BLOOD        Bantam
## 4                      Delia Owens WHERE THE CRAWDADS SING        Putnam
## 5                  James Patterson      TARGET: ALEX CROSS Little, Brown
## 6                   Michelle Obama                BECOMING         Crown
## 7             Charles Krauthammer,     THE POINT OF IT ALL   Crown Forum
## 8                    Tara Westover                EDUCATED  Random House
## 9  Bill O'Reilly and Martin Dugard          KILLING THE SS          Holt
## 10               Yuval Noah Harari                 SAPIENS        Harper
##    weeks_on_list
## 1              9
## 2             10
## 3              5
## 4             15
## 5              5
## 6              6
## 7              3
## 8             44
## 9             11
## 10            52

```