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. 

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

Installing jsonlite package:

First, we have to install the ā€œjsonliteā€ package by writing the following command:

install.packages("jsonlite")

Loading JSON data :

We load the data using the fromJSON() function from the jsonlite package in R language:

library(jsonlite)

sample_data <- fromJSON("sample.json")

Converting JSON data to Dataframe :

The fromJSON() function returns a list in R. We have to convert this list to a dataframe by using the as.data.frame() function :

output_dataframe <- as.data.frame(sample_data)

R




# First of all we have to Load jsonlite package
library(jsonlite)
  
# Then we have to load JSON data
sample_data <- fromJSON("sample.json")
  
# Then convert  the JSON data to dataframe
output_dataframe <- as.data.frame(sample_data)
  
# At last 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.

    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.

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

...