Reading JSON file
The JSON text in R is enclosed within the curly braces surrounded by string. The fromJSON() method in the rjson package is used to convert the JSON data into a text string and returns the data as a list of strings by default. It takes JSON files as input in the parameter. Each key becomes the header and the values to which they correspond are displayed as strings under the row numbers. This method performs the deserialization of the JSON data. It converts the data into an equivalent R object.
Syntax: fromJSON(json-file)
R
# Importing rjson library library ( "rjson" ) # Declaring the json text json_text <- '{ "ID" : [ "1" , "2" , "3" , "4" , "5" ], "User_name" : [ "A" , "B" , "C" , "D" , "E" ], "Marks" : [34, 64, 24, 68, 76], "Branch" : [ "Commerce" , "Science" , "Humanities" , "Non-medical" , "Humanities" ] }' # Reading the json text data <- fromJSON (json_text) # Printing json data print ( "JSON data" ) print (data) |
Output:
[1] "JSON data" > print(data) $ID [1] "1" "2" "3" "4" "5" $User_name [1] "A" "B" "C" "D" "E" $Marks [1] 34 64 24 68 76 $Branch [1] "Commerce" "Science" "Humanities" "Non-medical" "Humanities"
Download and Parse JSON Using R
In this article, we are going to learn how to download and parse JSON using the R programming language.
JavaScript Object Notation is referred to as JSON. These files have the data in text form, which is readable by humans. The JSON files are open for reading and writing just like any other file. The “rjson” package must be installed in order to work with JSON files in R.
Run the below command in R to install rjson package:
install.packages("rjson")