library(httr)
library(jsonlite)
library(DT)

In this assignment, we are require to use the New York Times API and read the JSON data and transform in R dataframe. I have used NYT books API and generated the API KEY upon signing in the website.

The process to use the API is provided [here] (https://developer.nytimes.com/docs/books-product/1/overview)

Below code shows the format to call the API. The URL is concatinated with api-key.

url <- 'https://api.nytimes.com/svc/books/v3/lists/current/hardcover-fiction.json'
api_key <- 'TBbi8eCy7Amyr926FiHGJZlEXYcgVUWr'

main_url <- paste(url,'?api-key=',api_key, sep = '')

Reading the JSON content from the URL. Flatten function remove the level of hierrachy from a list. The new variable class type is LIST. We need to convert to data.frame

books_raw_list <- fromJSON(main_url, flatten = TRUE)
class(books_raw_list)
## [1] "list"

Creating the data frame from the JSON format list.

books_df <- data.frame(books_raw_list$results$books)

class(books_df)
## [1] "data.frame"

Selecting only couple of column header.

books_df_subset <- books_df[, c("rank", "title", "author", "price", "publisher", "primary_isbn13")]

books_df_subset
##    rank                title                                  author price
## 1     1 THE BOYS FROM BILOXI                            John Grisham  0.00
## 2     2     DEMON COPPERHEAD                      Barbara Kingsolver  0.00
## 3     3           FAIRY TALE                            Stephen King  0.00
## 4     4         LONG SHADOWS                          David Baldacci  0.00
## 5     5            DREAMLAND                         Nicholas Sparks  0.00
## 6     6            MAD HONEY Jodi Picoult and Jennifer Finney Boylan  0.00
## 7     7             THE MAZE                          Nelson DeMille  0.00
## 8     8               VERITY                          Colleen Hoover  0.00
## 9     9   OUR MISSING HEARTS                              Celeste Ng  0.00
## 10   10       LIBERATION DAY                         George Saunders  0.00
## 11   11   THE LAST CHAIRLIFT                             John Irving  0.00
## 12   12 THE CHRISTMAS SPIRIT                         Debbie Macomber  0.00
## 13   13       RIGHTEOUS PREY                           John Sandford  0.00
## 14   14 LESSONS IN CHEMISTRY                           Bonnie Garmus  0.00
## 15   15          THE WINNERS                         Fredrik Backman  0.00
##           publisher primary_isbn13
## 1         Doubleday  9780385548922
## 2            Harper  9780063251922
## 3          Scribner  9781668002179
## 4     Grand Central  9781538719824
## 5      Random House  9780593449554
## 6        Ballantine  9781984818386
## 7          Scribner  9781501101786
## 8     Grand Central  9781538739723
## 9     Penguin Press  9780593492543
## 10     Random House  9780525509592
## 11 Simon & Schuster  9781501189272
## 12       Ballantine  9780593500101
## 13           Putnam  9780593422472
## 14        Doubleday  9780385547345
## 15            Atria  9781982112790

DT::datatable allows us to search and limit display result.

DT::datatable(books_df_subset)