Convert JSON data to Dataframe using Rjson Package in R

The rjson package provides functions for converting JSON data to lists, vectors, and data frames and vice versa, and also for parsing and generating JSON data. 

Steps to convert JSON data to a Dataframe in R using the Rjson Package

Installing rjson package:

install.packages("rjson")

Loading JSON Data:

sample_data <- fromJSON(file = "sample.json")

Converting JSON data to Dataframe :

output_dataframe<- data.frame(matrix(unlist(sample_data), 
ncol = length(sample_data[[1]]), byrow = TRUE), stringsAsFactors = FALSE)

R




# Load rjson package
library(rjson)
  
# Load JSON data
sample_data <- fromJSON(file = "sample.json")
  
  
# Convert JSON data to dataframe
output_dataframe <- data.frame(matrix(unlist(sample_data),
                        ncol = length(sample_data[[1]]), byrow = TRUE),
                        stringsAsFactors = FALSE)
  
# Print the output dataframe
print(output_dataframe)


Output:

This output_dataframe will have three columns named name, age, and city, and three rows with the data from the JSON file.

      X1 X2         X3
1 Sowham 20    Kolkata
2 Kushal 19 Jalpaiguri
3  Rohan 21   Durgapur

Convert JSON data to Dataframe in R

In the field of Data Analysis, we have to manage data in various formats, one of which is JSON (JavaScript Object Notation). JSON is used for storing and exchanging data between different systems and is hugely used in web development.

In R Programming language, we have to work often with data in different formats and convert it to a necessary format for analysis. In this case, it is necessary to convert the JSON data to a Dataframe, which is a common data structure in R. This language has several packages that make it easy to work with JSON data. 

Let’s take a JSON data named sample.json

[
  {
    "name": "Sowham",
    "age": 20,
    "city": "Kolkata"
  },
  {
    "name": "Kushal",
    "age": 19,
    "city": "Jalpaiguri"
  },
  {
    "name": "Rohan",
    "age": 21,
    "city": "Durgapur"
  }
]

Now, let us see how we can convert JSON data into a dataframe using various packages in R.

Similar Reads

Convert JSON data to Dataframe using Jsonlite Package in R

The jsonlite package is used for interacting with SQLite databases using JSON data. It is used to create, query, insert, update, and delete data in an SQLite database using a flexible interface for handling complex data structures....

Convert JSON data to Dataframe using RJSONIO Package in R

...

Convert JSON data to Dataframe using Rjson Package in R

The RJSONIO package is used for working with JSON data. It provides functions for converting between JSON data and R objects, as well as for parsing and generating JSON data....

Convert JSON data to Dataframe using Tidyjson Package in R

...