For week 7, I chose three books under data sicence and statistical learning. I manually created an HTML file and a JSON file that had the same book info.
The goal is to import both files into R, change them into data frames, and compare results.
library(rvest)
library(jsonlite)
- rvest lets me to extract the table from my HTML
file.
html_page <- read_html(
"https://raw.githubusercontent.com/muntasirmehran13-glitch/DATA607-Week7-HTML-JSON/main/books.html"
)
html_df <- html_page |>
html_element("table") |>
html_table()
html_df <- as.data.frame(html_df)
html_df
## Title
## 1 R for Data Science
## 2 Python for Data Analysis
## 3 An Introduction to Statistical Learning
## Authors
## 1 Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund
## 2 Wes McKinney
## 3 Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani
## Year Publisher Edition
## 1 2023 O'Reilly Media 2
## 2 2022 O'Reilly Media 3
## 3 2021 Springer 2
-jsonlite lets me read the JSON file and convert it into
a data frame.
json_df <- fromJSON(
"https://raw.githubusercontent.com/muntasirmehran13-glitch/DATA607-Week7-HTML-JSON/main/books.json"
)
json_df <- as.data.frame(json_df)
json_df
## Title
## 1 R for Data Science
## 2 Python for Data Analysis
## 3 An Introduction to Statistical Learning
## Authors
## 1 Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund
## 2 Wes McKinney
## 3 Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani
## Year Publisher Edition
## 1 2023 O'Reilly Media 2
## 2 2022 O'Reilly Media 3
## 3 2021 Springer 2
I had to standardize the column order and data types before verifying if the two data frames coincide.
columns <- c("Title", "Authors", "Year",
"Publisher", "Edition")
html_df <- html_df[, columns]
json_df <- json_df[, columns]
html_df$Year <- as.integer(html_df$Year)
json_df$Year <- as.integer(json_df$Year)
html_df$Edition <- as.integer(html_df$Edition)
json_df$Edition <- as.integer(json_df$Edition)
rownames(html_df) <- NULL
rownames(json_df) <- NULL
identical(html_df, json_df)
## [1] TRUE
Ultimately, the assignment showed me how HTML and JSON can indicate identical information using different frameworks.
While HTML formats data within a table, JSON organizes the information as structured entities.
After importing and standardizing both datasets, I am able to compare their contents in R to ensure that the information is consistent throughout formats.
The HTML and JSON files are publicly available in my GitHub repository: