Open In App

How to read JSON files in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see How to read a JSON file in R through an example, and also we will have a look at how to convert JSON data into a data frame. As we know, in the R language, we have to install different packages to deal with other things, so the first step is nothing but installing the package rjson.

Read JSON files in R

Step 1: To read a file in R language, we must install a package called rjson.So to install the package, we will use a function called install. packages().

R




install.packages("rjson",  dependencies = T)


Step 2: In this step, we need to load or store the package, and for that, we can use the native function library. It loads the rjson package or stores the rjson package for further use. The below code in the R Programming Language is used to store the rjson package.

R




library(rjson)


We can see that the library is loaded successfully

Step 3: In this step, Copy the information below into a text editor, such as Notepad, to create a JSON file. Select “all files” (*.*) as the file type and save the file with a.json extension.

we need to create a data file, or JSON file in this example, we are using the below datafile. The important thing about the data file is the extension of the file must be .json

{

“CollegeID”: [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”],

“Name”: [“Priyanshu”, “Yashodhra”, “Abhishek”, “Tanmay”, “Samriddha”, “Anjali”, “Samrat”, “Nikhil”],

“SubjectCode”: [“123”, “67”, “62”, “77”, “81”, “56”, “92”, “22”],

“CourseStartDate”: [“02/08/2018”, “09/03/2018”, “08/05/2018”, “05/11/2019”, “03/06/2019”, “05/11/2019”, “07/10/2020”, “06/07/2015”],

“StudentDepartment”: [“IT”, “Computer”, “ENTC”, “Electronics”, “Electrical”, “Civil”, “Chemical”, “Mechanical”]

}

Step 4: Now, in this step, we have to read the file that has JSON formatted data, and for that, we will use a function called fromJSON(). rjson provides this function which reads the file with JSON formatted data.

Syntax : fromJSON(path_of_a_file)

The below code in the R language is helpful for the fromJSON() function. Link to the json file.

R




JsonData <- fromJSON(file = 'C:\\Users\\GFG19565\\Downloads\\gfg.jason')
print(JsonData)


Output:

$CollegeID
[1] 1 2 3 4 5 6 7 8

$Name
[1] "Priyanshu" "Yashodhra" "Abhishek" "Tanmay" "Samriddha"
[6] "Anjali" "Samrat" "Nikhil"

$SubjectCode
[1] 123 67 62 77 81 56 92 22

$CourseStartDate
[1] "2018-02-08" "2018-09-03" "2018-08-05" "2019-05-11" "2019-03-06"
[6] "2019-05-11" "2020-07-10" "2015-06-07"

$StudentDepartment
[1] "IT" "Computer" "ENTC" "Electronics"
[5] "Electrical" "Civil" "Chemical" "Mechanical"

Step 5: Now, in this step, we will see how to convert JSON data into a dataframe, and for this, we will use a function called as.data.frame(). This function helps us to convert JSON data into a dataframe.

Syntax : as.data.frame(json_data)

R




jsonData_to_DataFrame <- as.data.frame(JsonData)


Step 6: Now you can see that the new object is being named ‘jsonData_to_Data…’

Step 7: In this, we need to click on that to see that our data is in tabular form with rows and columns.

R




jsonData_to_DataFrame


Output:

  CollegeID      Name SubjectCode CourseStartDate StudentDepartment
1 1 Priyanshu 123 2018-02-08 IT
2 2 Yashodhra 67 2018-09-03 Computer
3 3 Abhishek 62 2018-08-05 ENTC
4 4 Tanmay 77 2019-05-11 Electronics
5 5 Samriddha 81 2019-03-06 Electrical
6 6 Anjali 56 2019-05-11 Civil
7 7 Samrat 92 2020-07-10 Chemical
8 8 Nikhil 22 2015-06-07 Mechanical

jsonlite Package in R

The jsonlite package in R is designed to parse, generate, and work with JSON data. It provides functions for converting R objects to JSON format and vice versa. Some key functions include.

R




# Install the jsonlite package
install.packages("jsonlite")
 
# Load the jsonlite package into your R session
library(jsonlite)


Reading JSON from a File

R




# Read JSON from a file
json_data <- fromJSON("C:\\Users\\GFG19565\\Downloads\\GFG1.json")
json_data


Output:

$CollegeID
[1] "1" "2" "3" "4" "5" "6" "7" "8"
$Name
[1] "Priyanshu" "Yashodhra" "Abhishek" "Tanmay" "Samriddha"
[6] "Anjali" "Samrat" "Nikhil"
$SubjectCode
[1] "123" "67" "62" "77" "81" "56" "92" "22"
$CourseStartDate
[1] "02/08/2018" "09/03/2018" "08/05/2018" "05/11/2019" "03/06/2019"
[6] "05/11/2019" "07/10/2020" "06/07/2015"

Convert to data frame

R




# Convert to data frame
my_data_frame <- as.data.frame(json_data)
 
# Print the resulting data frame
print(my_data_frame)


Output:

  CollegeID      Name SubjectCode CourseStartDate StudentDepartment
1 1 Priyanshu 123 02/08/2018 IT
2 2 Yashodhra 67 09/03/2018 Computer
3 3 Abhishek 62 08/05/2018 ENTC
4 4 Tanmay 77 05/11/2019 Electronics
5 5 Samriddha 81 03/06/2019 Electrical
6 6 Anjali 56 05/11/2019 Civil
7 7 Samrat 92 07/10/2020 Chemical
8 8 Nikhil 22 06/07/2015 Mechanical

jsonlite is known for its high performance. It is written in C and offers a fast JSON parser. This makes it suitable for working with large JSON datasets.

While rjson is generally considered slower than jsonlite, the difference may not be significant for small to moderately sized datasets.



Last Updated : 22 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads