Interface for JSON Data

Overview

Overview

The New York Times web site provides a rich set of APIs, as described here: https://developer.nytimes.com/apis

The 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.

Packages

library(knitr)
library(jsonlite)
library(httr)
library(glue)
library(dplyr)
library(shiny)

Parameters

Construct an interface in R to read in the JSON data, and transform it into an R Data Frame.

  1. Construct URL Parameter with the desired data to be obtained
# https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json?api-key=yourkey

2.- Obtain Json File from API

Json

Read API

For this project I chose to use the Books API. In order to obtain the information we need to specify certain parameters to obtain specific data (as opposed to all the data available)

url_string <- "https://api.nytimes.com/svc/books/v3/lists/{date}/{list}.json?api-key={api_key}"

Parameters

The search parameters for are as follows:

  • List -> Hardcover-nonfiction Books
  • Date -> Which are the best sellers during Covid - April 2020
api_key <- Sys.getenv("NYT_TOKEN")
list <- "hardcover-nonfiction"
date <- "2020-04-01"

Once parameters are set - we use JSONlite and HTTR to create a function and “GET” the information we requested in the parameters which is inside the the content variable.

raw.data <- fromJSON(rawToChar(GET(glue(url_string))$content))
book.data <- raw.data$results$books

Dataframe

For the purpose of this project we will work with the following variables.

  • Title
  • Author
  • Weeks on list
  • Description
  • Book Image
book.rank.df <-
  book.data[,c("rank",
               "weeks_on_list",
               "title",
               "author",
               "description",
               "book_image")]

Final Result

kable(head(book.rank.df,5),caption = "NYT Hardcover Nonfiction Top 5")
NYT Hardcover Nonfiction Top 5
rank weeks_on_list title author description book_image
1 2 UNTAMED Glennon Doyle The activist and public speaker describes her journey of listening to her inner voice. https://storage.googleapis.com/du-prd/books/images/9781984801258.jpg
2 4 THE SPLENDID AND THE VILE Erik Larson An examination of the leadership of the prime minister Winston Churchill. https://storage.googleapis.com/du-prd/books/images/9780385348713.jpg
3 14 THE MAMBA MENTALITY Kobe Bryant Various skills and techniques used on the court by the late Los Angeles Lakers player. https://storage.googleapis.com/du-prd/books/images/9780374201234.jpg
4 109 EDUCATED Tara Westover The daughter of survivalists, who is kept out of school, educates herself enough to leave home for university. https://storage.googleapis.com/du-prd/books/images/9780399590504.jpg
5 7 OPEN BOOK Jessica Simpson with Kevin Carr O’Leary The singer, actress and fashion designer discloses times of success, trauma and addiction. https://storage.googleapis.com/du-prd/books/images/9780062899965.jpg