Convert list objects into JSON data
The list objects can also be coerced to the JSON data strings by the toJSON() method. It may even be a multi-level list.
Syntax: toJSON(data)
Parameter:
data – data to be converted into JSON data.
R
# Importing jsonlite library (jsonlite) # creating a data frame list_obj <- list (ob1= c (1: 3), ob2= "Yashika" , ob3= c ( TRUE , FALSE ), ob4= list (ele1= "x" , ele2= "y" )) print ( "List" ) print (list_obj) # converting to json object json_obj = toJSON (list_obj) # printing json print ( "JSON" ) print (json_obj) |
Output:
[1] "List" $ob1 [1] 1 2 3 $ob2 [1] "Yashika" $ob3 [1] TRUE FALSE $ob4 $ob4$ele1 [1] "x" $ob4$ele2 [1] "y" [1] "JSON" [1] "{\"ob1\":[1,2,3],\"ob2\":\"Yashika\",\"ob3\":[true,false],\"ob4\":{\"ele1\":\"x\",\"ele2\":\"y\"}}"
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")
Contact Us