library(rvest)
library(jsonlite)JSON & HTML
Approach
I will select three books I have read and record their titles, authors, and additional information. I will manually create an HTML file and a JSON file using Visual Studio Code, making sure both contain the same book information. Then, I will import both files into RStudio, convert them into data frames, and compare them to identify any differences.
Code Base
html_data <- read_html("Books.html")
html_df <- html_table(html_data)
head(html_df)[[1]]
# A tibble: 3 × 5
Title Authors `Publication Year` Publisher ISBN
<chr> <chr> <int> <chr> <dbl>
1 Salt, Fat, Acid, Heat Samin Nosrat 2017 Simon & … 9.78e12
2 The Food Lab J. Kenji Lopez-Alt 2015 W. W. No… 9.78e12
3 Joy of Cooking Irma S. Rombauer, … 2019 Scribner 9.78e12
json_data <- fromJSON("Books.json")
json_df <- json_data
head(json_df) title authors
1 Salt, Fat, Acid, Heat Samin Nosrat
2 The Food Lab J. Kenji Lopez-Alt
3 Joy of Cooking Irma S. Rombauer, Marion Rombauer Becker, Ethan Becker
publication_year publisher isbn
1 2017 Simon & Schuster 9781476753836
2 2015 W. W. Norton & Company 9780393081084
3 2019 Scribner 9781501169717
Conclusion
The HTML and JSON files contain similar book information, but there are some differences in how the data appears in R. For example, the authors in the JSON data frame display as <chr [1]> instead of their actual names. This would be tidied up to make sure the author names are not treated as a list.
Another difference is the ISBN format. In the HTML data frame, the ISBN appears in scientific notation (9.781477e+12), while the JSON data frame displays the full number without the hyphen.
Overall, this assignment showed that although HTML and JSON can store the same information, they may display and handle data differently when imported into R.