HTML-JSON

Author

Tenzin Lhamo Thakuri

Task

Select three books on a single subject of your choice. At least one book must have multiple authors. For each book, record the title, authors, and two to three additional attributes of your choosing (e.g., publication year, publisher, ISBN, genre, rating).

Using this information, manually create two data files:

An HTML file containing a table with the book data (e.g., books.html)

A JSON file representing the same data (e.g., books.json)

Unless you are already very comfortable with these formats, you are expected to create both files by hand to better understand their structure.

Next, write R code (using packages of your choice) to:

Load the HTML data into an R data frame

Load the JSON data into a separate R data frame

Compare the two data frames and determine whether they are identical

Deliverables books.html

books.json

R code that loads and compares both data sources

Package your complete solution in a Quarto (.qmd) file and publish it to rpubs.com.

Note: Publishing will require making your HTML and JSON files publicly accessible on the web.

Approach

I have selected the following three books on the subject of “Contemporary Fiction Novels”

I’m going to create the books.html and books.json file in visual studio code.

In the books.html file, I will create a table and insert all the data.

In the books.json file, I will create an array objects containing the same information.

I’m going to use rvest package to load an HTML file into R. To load json file into R, I’m going to use jsonlite package.

Anticipated challenge

This is my first time loading html file and json file into R. So I may face challenges in data formats.

Code

# rvest package to load the HTML data
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(rvest)

Attaching package: 'rvest'

The following object is masked from 'package:readr':

    guess_encoding
# jsonlite package to load the JSON data
library(jsonlite)

Attaching package: 'jsonlite'

The following object is masked from 'package:purrr':

    flatten
book_html_url <- "https://lhamo07.github.io/Data-607-Assignment/"

html_book_df <- read_html(book_html_url) %>% 
  # find the first <table> HTML tag
  html_element("table") %>% 
  # convert to R dataframe
  html_table()

html_book_df
# A tibble: 3 × 5
  title             author                      genre publication_year publisher
  <chr>             <chr>                       <chr>            <int> <chr>    
1 Never Never       Colleen Hoover, Tarryn Fis… Roma…             2015 Canary P…
2 The Correspondent Virginia Evans              Epis…             2025 Crown Pu…
3 Our Perfect Storm Carley Fortune              Cont…             2026 Berkley  

I published my HTML file on GitHub Pages by uploading it to the main branch. I named it index.html instead of books.html because GitHub Pages automatically uses that specific file as the website’s homepage.

# fromJSON() function reads the file and convert it into data frame
json_book_df<-fromJSON("https://raw.githubusercontent.com/lhamo07/Data-607-Assignment/refs/heads/main/Week7-html-json/books.json")
json_book_df
              title                        author
1       Never Never Colleen Hoover, Tarryn Fisher
2 The Correspondent                Virginia Evans
3 Our Perfect Storm                Carley Fortune
                               genre publication_year              publisher
1 Romantic Suspense, Mystery Fiction             2015        Canary Press,HQ
2                 Epistolary Fiction             2025 Crown Publishing Group
3        Contemporary Romance, Drama             2026                Berkley
glimpse(html_book_df)
Rows: 3
Columns: 5
$ title            <chr> "Never Never", "The Correspondent", "Our Perfect Stor…
$ author           <chr> "Colleen Hoover, Tarryn Fisher", "Virginia Evans", "C…
$ genre            <chr> "Romantic Suspense, Mystery Fiction", "Epistolary Fic…
$ publication_year <int> 2015, 2025, 2026
$ publisher        <chr> "Canary Press,HQ", "Crown Publishing Group", "Berkley"
glimpse(json_book_df)
Rows: 3
Columns: 5
$ title            <chr> "Never Never", "The Correspondent", "Our Perfect Stor…
$ author           <chr> "Colleen Hoover, Tarryn Fisher", "Virginia Evans", "C…
$ genre            <chr> "Romantic Suspense, Mystery Fiction", "Epistolary Fic…
$ publication_year <int> 2015, 2025, 2026
$ publisher        <chr> "Canary Press,HQ", "Crown Publishing Group", "Berkley"

Conclusion

I used R packages to load my book data from an HTML and a JSON file into two separate data frames. Taking a quick look at them, both data frames turned out to be completely identical.

Citation

[Large language model]. google.com. Accessed October 16, 2026. https://share.google/aimode/i1PC4Lg0G5g3Uz4Bg