Convert JSON data to Dataframe using RJSONIO 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.
Steps to convert JSON data to a Dataframe in R using the RJSONIO Package
Installing the RJSONIO package:
First, we have to install the “RJSONIO” package by using the following command:
install.packages("RJSONIO")
Loading JSON Data:
We load the sample.json data using the fromJSON function from the RJSONIO package in R language:
library(RJSONIO) sample_data <- fromJSON("sample.json")
Converting JSON data to Dataframe :
We have to convert this list to a dataframe by using the as.data.frame function :
output_dataframe <- as.data.frame(sample_data)
R
# Load RJSONIO package library (RJSONIO) # Load JSON data sample_data <- fromJSON ( "sample.json" ) # Convert JSON data to dataframe output_dataframe <- as.data.frame (sample_data) # Print the output dataframe print (output_dataframe) |
Output:
name age city name.1 age.1 city.1 name.2 age.2 city.2 1 Sowham 20 Kolkata Kushal 19 Jalpaiguri 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.