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




# 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)
  
# converting data into data frame
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 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")

Similar Reads

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 :...

Convert JSON text into a data frame

...

Convert data objects into the JSON text object

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....

Convert list objects into JSON data

...