3.7 Import JSON and XML
JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are standard hierarchical data formats often obtained from APIs or other data sources. For JSON, we use the jsonlite package. Here, fromJSON() reads JSON files or text into R lists or data frames. Note that the path does not need to refer to a place on your computer but can also be an address to an online API:
library(jsonlite)
my_data_df <- fromJSON("path/to/your/data.json", flatten = TRUE) # Here, ``flatten = TRUE`` flattens the object into a data frameFor XML, we use the xml2 package. It provides functions like read_xml() or read_html() to parse files/URLs, and xml_find_all() (with XPath expressions), xml_text(), xml_attr() etc., to navigate and extract data: