Convert JSON data to Dataframe using Tidyjson Package in R
The Tidyjson package along with dplyr package provides functions to convert JSON data into a dataframe in R.
Steps to convert JSON data to a Dataframe in R using the Tidyjson Package:
Installing tidyjson package:
install.packages("tidyjson")
Loading JSON Data:
json_data <- jsonlite::fromJSON("sample.json")
Converting JSON data to Dataframe :
df <- as.data.frame(json_data)
R
# Step 1: Install and load the RJSONIO package install.packages ( "tidyjson" ) library (tidyjson) library (dplyr) # Step 2: Read the JSON data json_data <- jsonlite:: fromJSON ( "sample.json" ) # Step 3: Convert the R object to a data frame df <- as.data.frame (json_data) # Step 4: View the data frame head (df) |
Output:
name age city 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.