Working with JSON Files in R Programming

Working with JSON Files in detail

JSON (JavaScript Object Notation) is a widely used data format that stores information in a structured and readable manner, using text-based key-value pairs. Just like other files, JSON files can be both read and written in R. To work with JSON files in R, we need to install and use the rjson package.

Common JSON Operations in R

Using the rjson package, we can perform various tasks, including:

  • Installing and loading the rjson package
  • Creating a JSON file
  • Reading data from a JSON file
  • Writing data into a JSON file
  • Converting JSON data into a dataframe
  • Extracting data from URLs
Installing and Loading the rjson Package

To use JSON functionality in R, install the rjson package using the command below:

install.packages("rjson")

Once installed, load the package into the R environment using:

library("rjson")

To create a JSON file, follow these steps:

  1. Open a text editor (such as Notepad) and enter data in the JSON format.
  2. Save the file with a .json extension (e.g., sample.json).

Example JSON Data:

{
   "EmployeeID":["101","102","103","104","105"],
   "Name":["Amit","Rohit","Sneha","Priya","Karan"],
   "Salary":["55000","63000","72000","80000","59000"],
   "JoiningDate":["2015-03-25","2018-07-10","2020-01-15","2017-09-12","2019-05-30"],
   "Department":["IT","HR","Finance","Operations","Marketing"]
}
Reading a JSON File in R

The fromJSON() function helps read and parse JSON data from a file. The extracted data is stored as a list by default.

Example Code:

# Load required package
library("rjson")

# Read the JSON file from a specified location
data <- fromJSON(file = "D:\\sample.json")

# Print the data
print(data)

Output:

$EmployeeID
[1] "101" "102" "103" "104" "105"

$Name
[1] "Amit"   "Rohit"   "Sneha"   "Priya"   "Karan"

$Salary
[1] "55000" "63000" "72000" "80000" "59000"

$JoiningDate
[1] "2015-03-25" "2018-07-10" "2020-01-15" "2017-09-12" "2019-05-30"

$Department
[1] "IT"         "HR"         "Finance"    "Operations" "Marketing"
Writing Data to a JSON File in R

To write data into a JSON file, we first convert data into a JSON object using the toJSON() function and then use the write() function to store it in a file.

Example Code:

# Load the required package
library("rjson")

# Creating a list with sample data
data_list <- list(
  Fruits = c("Apple", "Banana", "Mango"),
  Category = c("Fruit", "Fruit", "Fruit")
)

# Convert list to JSON format
json_output <- toJSON(data_list)

# Write JSON data to a file
write(json_output, "output.json")

# Read and print the created JSON file
result <- fromJSON(file = "output.json")
print(result)

Output:

$Fruits
[1] "Apple"  "Banana" "Mango"

$Category
[1] "Fruit"  "Fruit"  "Fruit"
Converting JSON Data into a Dataframe

In R, JSON data can be transformed into a dataframe using as.data.frame(), allowing easy manipulation and analysis.

Example Code:

# Load required package
library("rjson")

# Read JSON file
data <- fromJSON(file = "D:\\sample.json")

# Convert JSON data to a dataframe
json_df <- as.data.frame(data)

# Print the dataframe
print(json_df)

Output:

EmployeeID   Name Salary JoiningDate  Department
1       101   Amit  55000  2015-03-25          IT
2       102  Rohit  63000  2018-07-10          HR
3       103  Sneha  72000  2020-01-15     Finance
4       104  Priya  80000  2017-09-12 Operations
5       105  Karan  59000  2019-05-30  Marketing
Working with JSON Data from a URL

JSON data can be extracted from online sources using either the jsonlite or RJSONIO package.

Example Code:

# Load the required package
library(RJSONIO)

# Fetch JSON data from a URL
data_url <- fromJSON("https://api.publicapis.org/entries")

# Extract specific fields
API_Names <- sapply(data_url$entries, function(x) x$API)

# Display first few API names
head(API_Names)

Output:

[1] "AdoptAPet" "Axolotl" "Cat Facts" "Dog CEO" "Fun Translations" 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *