Working with HTML and JSON

Author

Ozge Gundogan

Published

October 18, 2026

Introduction

In this assignment, I selected three books about physical chemistry. I manually created an HTML file and a JSON file containing the same information about each book, including the title, authors, publication year, publisher, and subject.

The main goal of this assignment is to understand how HTML and JSON store data and how to import these formats into R data frames.

Planned Approach

First, I will create an HTML file containing a table with information about three books. Then, I will create a JSON file using the same data.

Next, I will use the rvest package to read the HTML table and the jsonlite package to read the JSON file. I will convert both datasets into separate R data frames. Finally, I will check whether the two data frames contain identical data and examine their structures.

Anticipated Challenges

One possible challenge is understanding the different structures of HTML and JSON files and ensuring that both files contain exactly the same information.

Code Base

Load Required Packages

To read and process the HTML and JSON files, I will use two R packages. The rvest package will be used to extract the table from the HTML file, while the jsonlite package will be used to import the JSON data into R.

library(rvest)
library(jsonlite)

Load HTML Data

First, I will use the read_html() function to read the HTML file. Then, I will extract the book table using html_element() and html_table(). Finally, I will convert the extracted table into a data frame.

html_data <- read_html("https://raw.githubusercontent.com/OzgeG01/DATA607-Assignment/refs/heads/main/books.html")

html_table <- html_data |>
  html_element("table") |>
  html_table()

html_df <- as.data.frame(html_table)

html_df
                                     Title                            Authors
1       Fundamentals of Physical Chemistry   Samuel H. Maron, Jerome B. Lando
2 Physical Chemistry: A Molecular Approach Donald A. McQuarrie, John D. Simon
3                       Physical Chemistry                       P. W. Atkins
  Publication_Year                Publisher            Subject
1             1974                Macmillan Physical Chemistry
2             1997 University Science Books Physical Chemistry
3             1978                  Freeman Physical Chemistry

Load JSON Data

Next, I will use the fromJSON() function from the jsonlite package to read the JSON file. I will then convert the imported data into a separate data frame so that it can be compared with the HTML data frame.

json_data <- fromJSON("https://raw.githubusercontent.com/OzgeG01/DATA607-Assignment/refs/heads/main/books.json")

json_df <- as.data.frame(json_data)

json_df
                                     Title                            Authors
1       Fundamentals of Physical Chemistry   Samuel H. Maron, Jerome B. Lando
2 Physical Chemistry: A Molecular Approach Donald A. McQuarrie, John D. Simon
3                       Physical Chemistry                       P. W. Atkins
  Publication_Year                Publisher            Subject
1             1974                Macmillan Physical Chemistry
2             1997 University Science Books Physical Chemistry
3             1978                  Freeman Physical Chemistry

Compare HTML and JSON Data

After importing both files, I will use the identical() function to compare the two data frames. This function checks whether the data frames are exactly the same, including their values, column names, data types, and other attributes.

identical(html_df, json_df)
[1] TRUE

The identical() function returned TRUE, indicating that the two data frames are identical. This confirms that the HTML and JSON files were successfully imported into R and produced the same data structure and values. Although HTML and JSON store information in different formats, both files contain the same book information.

Check Data Structures

Although the identical() function returned TRUE, I will also examine the structures of both data frames using the str() function. This will allow me to verify the number of observations, column names, and data types in each data frame.

str(html_df)
'data.frame':   3 obs. of  5 variables:
 $ Title           : chr  "Fundamentals of Physical Chemistry" "Physical Chemistry: A Molecular Approach" "Physical Chemistry"
 $ Authors         : chr  "Samuel H. Maron, Jerome B. Lando" "Donald A. McQuarrie, John D. Simon" "P. W. Atkins"
 $ Publication_Year: int  1974 1997 1978
 $ Publisher       : chr  "Macmillan" "University Science Books" "Freeman"
 $ Subject         : chr  "Physical Chemistry" "Physical Chemistry" "Physical Chemistry"
str(json_df)
'data.frame':   3 obs. of  5 variables:
 $ Title           : chr  "Fundamentals of Physical Chemistry" "Physical Chemistry: A Molecular Approach" "Physical Chemistry"
 $ Authors         : chr  "Samuel H. Maron, Jerome B. Lando" "Donald A. McQuarrie, John D. Simon" "P. W. Atkins"
 $ Publication_Year: int  1974 1997 1978
 $ Publisher       : chr  "Macmillan" "University Science Books" "Freeman"
 $ Subject         : chr  "Physical Chemistry" "Physical Chemistry" "Physical Chemistry"

The str() function shows that both data frames contain 3 observations and 5 variables. The column names and data types are also the same in both data frames. The Publication_Year column is stored as an integer, while the other four columns are stored as character variables. These results further confirm that the HTML and JSON files were imported consistently into R.

Conclusion

In this assignment, I manually created HTML and JSON files containing information about three physical chemistry books. I used the rvest and jsonlite packages to import the data from both files into separate R data frames.

The identical() function returned TRUE, confirming that both data frames contain identical data. I also examined their structures using the str() function and verified that the column names and data types were the same.

This assignment helped me understand how HTML and JSON store data differently and how both formats can be imported and compared in R.

Citation

I used ChatGPT (GPT-5.6 Luna), developed by OpenAI, to help me understand some of the functions I used in this assignment and to check my work.

OpenAI. (2026). ChatGPT (GPT-5.6 Luna) [Large language model]. [https://chatgpt.com/ Accessed October 18, 2026].

Video Explainer

Link: