File Handling in R
Estimated reading: 4 minutes 292 views

R – Reading XML and JSON Data (with Code Explanation)


Introduction – Parsing Web & Nested Data in R

Modern data often comes in structured hierarchical formats like XML (used in config and APIs) or JSON (standard for web APIs, NoSQL). R provides packages to import and parse both XML and JSON easily into usable data frames or lists.

In this guide, you’ll learn:

  • How to read and parse XML and JSON files in R
  • Convert them into structured R objects
  • Understand how to extract nested data with practical code and explanations

1. Reading XML Files in R

Step 1: Install and Load the xml2 Package

install.packages("xml2")
library(xml2)

Step 2: Read an XML File

xml_data <- read_xml("books.xml")

Explanation:

  • read_xml() reads the XML structure into an R xml_document object.
  • books.xml is your local or web-based XML file.

Step 3: Explore XML Content

xml_name(xml_data)        # Root node name
xml_children(xml_data)    # Get child nodes

Step 4: Extract Specific Tags

Assume this structure:

<book>
  <title>R Programming</title>
  <author>John Doe</author>
</book>
titles <- xml_find_all(xml_data, ".//title")
xml_text(titles)

Explanation:

  • xml_find_all() selects all <title> tags.
  • xml_text() extracts the text values inside those nodes.

Output:

[1] "R Programming"

2. Reading JSON Files in R

Step 1: Install and Load the jsonlite Package

install.packages("jsonlite")
library(jsonlite)

Step 2: Read a JSON File

json_data <- fromJSON("students.json")

Explanation:

  • fromJSON() reads the JSON file and automatically converts it into a list or data frame, depending on structure.
  • Perfect for nested objects like:
{
  "name": "Alice",
  "scores": [90, 85, 92]
}

Step 3: Access JSON Data

json_data$name      # "Alice"
json_data$scores    # 90 85 92

Step 4: Nested JSON Structure Example

{
  "students": [
    { "name": "Alice", "age": 23 },
    { "name": "Bob", "age": 25 }
  ]
}
json_data <- fromJSON("students.json")
df <- json_data$students

Output:

   name age
1 Alice  23
2   Bob  25

Load JSON or XML from Web URLs

Read JSON from URL

fromJSON("https://api.example.com/data.json")

Read XML from URL

read_xml("https://example.com/data.xml")

Works directly with API endpoints and web-accessible files.


Handling Common Issues

ProblemFix or Tip
File not foundUse getwd() to check working directory
Invalid XML/JSON structureValidate using online validators before parsing
Nested structure too deepUse str() to explore and extract manually
Encoding problemsUse encoding = "UTF-8" in readLines() if needed

Summary – Recap & Next Steps

R makes it simple to work with web and hierarchical data like XML and JSON. These formats are widely used in web APIs, data exchange, and configuration files.

Key Takeaways:

  • Use xml2::read_xml() and jsonlite::fromJSON() for parsing
  • Access nested content using indexing or helper functions
  • Use xml_text(), xml_find_all(), str(), and $ for exploration
  • Load from local or web sources with ease
  • Convert structured data into data frames for analysis

Real-World Relevance:
Used in API integrations (e.g., weather, finance, healthcare), configuration systems, cloud dashboards, and data science automation.


FAQs – Reading XML and JSON in R

How do I read an XML file from a URL in R?
Use read_xml():

read_xml("https://example.com/file.xml")

What package is best for parsing JSON in R?
jsonlite is widely used and converts JSON into data frames or lists easily.

How do I convert nested JSON into a data frame?
Use fromJSON() and access nested keys:

df <- fromJSON("file.json")$students

Can I write back to JSON or XML in R?
Yes:

  • Use toJSON() from jsonlite for JSON.
  • Use xml2::write_xml() or writeLines() for XML.

How do I validate XML or JSON before reading?
Use online tools like jsonlint.com or xmlvalidation.com to verify structure.


Share Now :
Share

R – Reading XML / JSON

Or Copy Link

CONTENTS
Scroll to Top