Assignment – Working with XML and JSON in R

Pick three of your favorite books on one of your favorite subjects. At least one of the books should have more than one author. For each book, include the title, authors, and two or three other attributes that you find interesting.

Take the information that you’ve selected about these three books, and separately create three files which store the book’s information in HTML (using an html table), XML, and JSON formats (e.g. “books.html”, “books.xml”, and “books.json”). To help you better understand the different file structures, I’d prefer that you create each of these files “by hand” unless you’re already very comfortable with the file formats.

Write R code, using your packages of choice, to load the information from each of the three sources into separate R data frames. Are the three data frames identical?

Your deliverable is the three source files and the R code. If you can, package your assignment solution up into an .Rmd file and publish to rpubs.com. [This will also require finding a way to make your three text files accessible from the web].

Files

JSON

https://github.com/nk014914/Data-607/blob/main/book_data.json

Each table is comprised of the following columns: Title, Author, Genre, Pages, and Publish Year. Because my chosen books only had one author per book, I created the genre column which can contain more than one genre per book.

#load the packages
library(XML)
## Warning: package 'XML' was built under R version 4.2.3
library(jsonlite)
library(RCurl)
## Warning: package 'RCurl' was built under R version 4.2.3

I’m not exactly sure why but loading my HTML into R data frame had “NULL.” in front of each column name, even though it is not in the html code itself. So for that reason I removed it here to clean up the data frame columns.

#load html data
book_html <- getURL('https://raw.githubusercontent.com/nk014914/Data-607/main/book_data_v2.html')

book_html
## [1] "<!doctype html>\n\n<html>\n\n<head>\n\n<title>Book Summary</title>\n\n</head>\n\n<body>\n\n<h1>Book Summary</h1>\n\n<table border=\"1\">\n\n    <tr>\n\n        <th>title</th>\n\n        <th>author</th>\n\n        <th>genre</th>\n\n        <th>pages</th>\n\t\n\t<th>publish_year</th>\n\n    </tr>\n\n    <tr>\n\n    <td>Harry Potter and the Goblet of Fire</td>\n    <td>J.K Rowling</td>\n    <td>Fantasy, Action</td>\n    <td>752</td>\n    <td>2000</td>\n\n    </tr>\n\n    <tr>\n\n    <td>Lovely Bones</td>\n    <td>Alice Sebold</td>\n    <td>Mystery</td>\n    <td>328</td>\n    <td>2002</td>\n\n    <tr>\n\n    <td>Incantation</td>\n    <td>Alice Hoffman</td>\n    <td>History</td>\n    <td>192</td>\n    <td>2006</td>\n\n    </tr>\n\n</table>\n\n</body>\n\n</html>\n"
#view table
html <- readHTMLTable(book_html)
df_html <- data.frame(html)

#remove null in column names
colnames(df_html)[1] = "title"
colnames(df_html)[2] = "author"
colnames(df_html)[3] = "genre"
colnames(df_html)[4] = "pages"
colnames(df_html)[5] = "publish_year"
df_html
##                                 title        author           genre pages
## 1 Harry Potter and the Goblet of Fire   J.K Rowling Fantasy, Action   752
## 2                        Lovely Bones  Alice Sebold         Mystery   328
## 3                         Incantation Alice Hoffman         History   192
##   publish_year
## 1         2000
## 2         2002
## 3         2006

With the XML package, we can use xmlParse and xmlToDataFrame to help us make the xml into a list and dataframe.

#load xml data

book_xml <- getURL('https://raw.githubusercontent.com/nk014914/Data-607/main/book_data.xml')

book_xml
## [1] "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \r\n<book_data>\r\n<book>\r\n<title>Harry Potter and the Goblet of Fire</title>\r\n<author>J.K Rowling</author>\r\n<genre>Fantasy, Action</genre>\r\n<pages>752</pages>\r\n<publish_year>2000</publish_year>\r\n</book>\r\n<book>\r\n<title>Lovely Bones</title>\r\n<author>Alice Sebold</author>\r\n<genre>Mystery</genre>\r\n<pages>328</pages>\r\n<publish_year>2002</publish_year>\r\n</book>\r\n<book>\r\n<title>Incantation</title>\r\n<author>Alice Hoffman</author>\r\n<genre>History</genre>\r\n<pages>192</pages>\r\n<publish_year>2006</publish_year>\r\n</book> \r\n</book_data>"
#view table
xml <- xmlParse(book_xml)
df_xml <- xmlToDataFrame(xml)
df_xml
##                                 title        author           genre pages
## 1 Harry Potter and the Goblet of Fire   J.K Rowling Fantasy, Action   752
## 2                        Lovely Bones  Alice Sebold         Mystery   328
## 3                         Incantation Alice Hoffman         History   192
##   publish_year
## 1         2000
## 2         2002
## 3         2006

We will use the jsonlite package for loading this data.

#load json data

book_json <- getURL('https://raw.githubusercontent.com/nk014914/Data-607/main/book_data.json')

book_json
## [1] "[\r\n  {\r\n    \"title\": \"Harry Potter and the Goblet of Fire\",\r\n    \"author\": \"J.K Rowling\",\r\n    \"genre\": [\r\n      \"Fantasy\",\r\n      \"Action\"\r\n    ],\r\n    \"pages\": \"752\",\r\n    \"publish year\": 2000\r\n  },\r\n  {\r\n    \"title\": \"Lovely Bones\",\r\n    \"author\": \"Alice Sebold\",\r\n    \"genre\": \"Mystery\",\r\n    \"pages\": \"328\",\r\n    \"publish year\": 2002\r\n  },\r\n  {\r\n    \"title\": \"Incantation\",\r\n    \"author\": \"Alice Hoffman\",\r\n    \"genre\": \"History\",\r\n    \"pages\": \"192\",\r\n    \"publish year\": 2006\r\n  }\r\n]"
#view table
df_json <- fromJSON(book_json)
df_json
##                                 title        author           genre pages
## 1 Harry Potter and the Goblet of Fire   J.K Rowling Fantasy, Action   752
## 2                        Lovely Bones  Alice Sebold         Mystery   328
## 3                         Incantation Alice Hoffman         History   192
##   publish year
## 1         2000
## 2         2002
## 3         2006

Conclusion

After loading all three data sets (HTML, XML, and JSON) and proper formatting for the html table, we can see that the three data frames are identical. JSON was the easiest to load as it didn’t require any formatting or extra conversions, while HTML required both before putting it into a data frame.