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.
How to read JSON files in R
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.
Contact Us