Convert R objects to/from JSON in jsonlite
Last Updated :
16 Sep, 2022
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")
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
library ( "jsonlite" )
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" ]
}'
data <- fromJSON (json_text)
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 JSON text into a data frame
The JSON text can also be converted to a data frame. The R object can be used to visualize data in a much more organized tabular structure. After the conversion of the JSON text, it can be subjected to the as.data.frame() method which coerces it into a data frame object. The keys of the JSON text are displayed as column headers of the data frame and the values are the cell values.
Syntax: as.data.frame(data)
Parameter:
data – Data to be converted into data frame
R
library ( "jsonlite" )
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" ]
}'
data <- fromJSON (json_text)
data_frame <- as.data.frame (data)
print ( "JSON dataframe" )
print (data_frame)
|
Output:
ID User_name Marks Branch
1 1 A 34 Commerce
2 2 B 64 Science
3 3 C 24 Humanities
4 4 D 68 Non-medical
5 5 E 76 Humanities
Convert data objects into the JSON text object
The toJSON() method can be used to convert the data objects into the JSON text object. The method has the following syntax :
Syntax: toJSON(R-object-text)
Parameters :
R-object-text – The data contained in R object.
R
library (rjson)
data_frame <- data.frame (col1= c (1: 5),
col2= letters [1:5],
col3= c ( "Commerce" ,
"Humanities" ,
"CSE" ,
"Commerce" ,
"Humanities" )
)
print ( "Data Frame" )
print (data_frame)
json_obj = toJSON (data_frame)
print ( "JSON" )
print (json_obj)
|
Output:
[1] "Data Frame"
col1 col2 col3
1 1 a Commerce
2 2 b Humanities
3 3 c CSE
4 4 d Commerce
5 5 e Humanities
[1] "JSON"
[1] "{\"col1\":[1,2,3,4,5],\"col2\":[\"a\",\"b\",\"c\",\"d\",\"e\"],\"col3\":[\"Commerce\",\"Humanities\",\"CSE\",\"Commerce\",\"Humanities\"]}"
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
library (jsonlite)
list_obj <- list (ob1= c (1: 3),
ob2= "Yashika" ,
ob3= c ( TRUE , FALSE ),
ob4= list (ele1= "x" ,
ele2= "y" ))
print ( "List" )
print (list_obj)
json_obj = toJSON (list_obj)
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\"}}"
Similar Reads
How to Convert JSON String to a JSON Object in Scala?
When working with JSON data in Scala, we may often need to convert a JSON string into a JSON object. This can be useful for parsing and manipulating JSON data effectively. This article focuses on discussing ways to convert JSON string to a JSON object. Table of Content Using the Built-in Parse Metho
3 min read
How to Convert JSON Object to CSV in JavaScript ?
JSON (JavaScript Object Notation) and CSV (Comma-Separated Values) are two widely used formats, each with its own strengths and applications. Fortunately, JavaScript provides powerful tools to facilitate the conversion process between these formats. These are the following approaches: Table of Conte
3 min read
Convert Generator Object To JSON In Python
JSON (JavaScript Object Notation) is a widely used data interchange format, and Python provides excellent support for working with JSON data. However, when it comes to converting generator objects to JSON, there are several methods to consider. In this article, we'll explore some commonly used metho
2 min read
Converting JSON text to JavaScript Object
Pre-requisite: JavaScript JSON JSON (JavaScript Object Notation) is a lightweight data-interchange format. As its name suggests, JSON is derived from the JavaScript programming language, but itâs available for use by many languages including Python, Ruby, PHP, and Java and hence, it can be said as l
3 min read
Convert class object to JSON in Python
In Python, class objects are used to organize complex information. To save or share this information, we need to convert it into a format like JSON, which is easy to read and write. Since class objects can't be saved directly as JSON, we first convert them into a dictionary (a data structure with ke
3 min read
Converting MultiDict to proper JSON
In this article, we will see what is MultiDict, and how do we convert a multidict into JSON files in Python. First, we will convert a multidict to a dictionary data type, and at last, we dump that dictionary into a JSON file. Functions Used : json.dump(): JSON module in Python module provides a meth
2 min read
How to Remove Empty Object from JSON in JavaScript ?
In JSON, empty objects can cause data inconsistency and processing issues. We will explore three different approaches filter method, forEach Loop, and for Loop to remove empty objects from JSON in JavaScript. Table of Content Using filter MethodUsing forEach LoopUsing for LoopUsing Array.reduce() Me
3 min read
Convert nested JSON to CSV in Python
In this article, we will discuss how can we convert nested JSON to CSV in Python. An example of a simple JSON file: As you can see in the example, a single key-value pair is separated by a colon (:) whereas each key-value pairs are separated by a comma (,). Here, "name", "profile", "age", and "locat
9 min read
How to Remove Element from JSON Object in JavaScript ?
In JavaScript, removing elements from a JSON object is important for modifying data structures dynamically. This object manipulation can help us to create dynamic web pages. The approaches to accomplish this task are listed and discussed below: Table of Content Using delete KeywordUsing filter Metho
2 min read
How to convert Ordereddict to JSON?
In this article, we will learn How to convert a nested OrderedDict to JSON? Before this we must go through some concepts: The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the d
3 min read