Parse JSON in R
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. 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. The method has the following syntax :
Syntax: fromJSON(json-text)
Parameter :
json-text – JSON content or file name.
R
# Importing jsonlite library ( "jsonlite" ) # 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"
Convert R objects to/from JSON in jsonlite
In this article, we are going to know how to convert R objects to/from JSON in jsonlite using the R programming language.
jsonlite package
The jsonlite package in R is used to simulate the conversion to and from the JSON data objects in R. It can be converted easily to other data objects. The package can be downloaded and installed into the R working space using the following command.
install.packages("jsonlite")