HTML and JSON

DATA 607 - Working with HTML and JSON

Author

Mohd Afwan Shaikh

Introduction

The purpose of this assignment is to practice working with HTML and JSON data formats in R. I manually created two files containing information about three books related to data science.

Each file contains the same five attributes: title, authors, publication year, publisher, and subject. I will load both files into separate R data frames and compare them to determine whether they are identical.

Load Required Packages

I used the rvest package to read HTML, the jsonlite package to read JSON, and the knitr package to display the data in tables.

library(rvest)
library(jsonlite)
library(knitr)

Load HTML Data

The HTML file contains a table with information about three data science books. I used the rvest package to extract the table and convert it into a data frame.

html_data <- read_html("books.html") |>
  html_element("table") |>
  html_table() |>
  as.data.frame()

kable(html_data, caption = "Books Loaded from HTML")
Books Loaded from HTML
Title Authors Year Publisher Subject
R for Data Science (2nd Edition) Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund 2023 O’Reilly Media Data Science
An Introduction to Statistical Learning (2nd Edition) Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani 2021 Springer Data Science
Python for Data Analysis (3rd Edition) Wes McKinney 2022 O’Reilly Media Data Science

Load JSON Data

The JSON file contains the same book information. I used the jsonlite package to import the data into a separate data frame.

json_data <- fromJSON("books.json") |>
  as.data.frame()

kable(json_data, caption = "Books Loaded from JSON")
Books Loaded from JSON
Title Authors Year Publisher Subject
R for Data Science (2nd Edition) Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund 2023 O’Reilly Media Data Science
An Introduction to Statistical Learning (2nd Edition) Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani 2021 Springer Data Science
Python for Data Analysis (3rd Edition) Wes McKinney 2022 O’Reilly Media Data Science

Compare the Data Frames

Before comparing the data frames, I checked their column names and data types to make sure they were consistent.

names(html_data)
[1] "Title"     "Authors"   "Year"      "Publisher" "Subject"  
names(json_data)
[1] "Title"     "Authors"   "Year"      "Publisher" "Subject"  
str(html_data)
'data.frame':   3 obs. of  5 variables:
 $ Title    : chr  "R for Data Science (2nd Edition)" "An Introduction to Statistical Learning (2nd Edition)" "Python for Data Analysis (3rd Edition)"
 $ Authors  : chr  "Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund" "Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani" "Wes McKinney"
 $ Year     : int  2023 2021 2022
 $ Publisher: chr  "O'Reilly Media" "Springer" "O'Reilly Media"
 $ Subject  : chr  "Data Science" "Data Science" "Data Science"
str(json_data)
'data.frame':   3 obs. of  5 variables:
 $ Title    : chr  "R for Data Science (2nd Edition)" "An Introduction to Statistical Learning (2nd Edition)" "Python for Data Analysis (3rd Edition)"
 $ Authors  : chr  "Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund" "Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani" "Wes McKinney"
 $ Year     : int  2023 2021 2022
 $ Publisher: chr  "O'Reilly Media" "Springer" "O'Reilly Media"
 $ Subject  : chr  "Data Science" "Data Science" "Data Science"

Data Cleaning

I noticed that the HTML file used a curly apostrophe in “O’Reilly Media,” while the JSON file used a straight apostrophe in “O’Reilly Media.”

Although both names look similar, R treats these characters differently. I standardized the apostrophes in the HTML data to make the values consistent.

html_data$Publisher <- gsub(
  "\u2019",
  "'",
  html_data$Publisher,
  fixed = TRUE
)

Comparing the Data Frames

After cleaning the data, I arranged the columns in the same order and used the identical() function to compare the two data frames.

json_data <- json_data[, names(html_data), drop = FALSE]

comparison <- identical(html_data, json_data)

comparison
[1] TRUE

Results

if (comparison) {
  cat("Result: TRUE - Both data frames are identical.")
} else {
  cat("Result: FALSE - The data frames are not identical.")
}
Result: TRUE - Both data frames are identical.

The comparison returned TRUE, confirming that both data frames contain the same information after standardizing the apostrophes.

Conclusion

This assignment demonstrated how HTML and JSON can represent the same information using different structures.

HTML organizes the book information into a table using rows and columns, while JSON represents the same information using objects and key-value pairs.

Using R, I imported both files into separate data frames and compared their contents. The comparison returned TRUE, confirming that the two data frames are identical after standardizing the publisher names.

This exercise helped me understand how to work with different data formats, identify small differences in text values, and prepare data for further analysis.