R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a web page. It often stores data as text in human readable format. R can read JSON file using the rjson package.

Install rjson Package

To install rjson package use the following command:

install.packages("rjson")

Create a JSON File

Copy the following code into notepad or any other text editor and save this file with a .json extension.

{
   "ID":["1","2","3","4","5" ],
   "Name":["Nikita","Deep","Aman","Nidhi","Sonu" ],
   "Salary":["50000","20000","25000","23000","30000" ],   
   "StartDate":[ "1/1/2018","9/23/2014","11/15/2017","5/11/2015","3/27/2019"],
   "Dept":[ "IT","Operations","IT","HR","Finance"]
}

Read the JSON File

fromJSON() function is used to read the JSON file in R. It is stored as a list in R.

Example:

# Load the package required package.
library("rjson")
result <- fromJSON(file = "Student.json")
# Print the result.
print(result)

Output:

$ID

[1] "1" "2" "3" "4" "5"

$Name

[1] "Nikita" "Deep"   "Aman"   "Nidhi"  "Sonu" 

$Salary

[1] "50000" "20000" "25000" "23000" "30000"

$StartDate

[1] "1/1/2018"   "9/23/2014"  "11/15/2017" "5/11/2015"  "3/27/2019"

$Dept

[1] "IT"         "Operations" "IT"         "HR"         "Finance"

Convert JSON to a Data Frame

We can convert the JSON data which is extracted from fromJson() function into the form of data frame. For this R provides a function i.e. as.data.frame() function.

Example:

# Load the package required to read JSON files.
library("rjson")
# Give the input file name to the function.
result <- fromJSON(file = "Student.json")
# Convert JSON file to a data frame.
df <- as.data.frame(result)
print(df)

Output:

  ID   Name Salary  StartDate       Dept
1  1 Nikita  50000   1/1/2018         IT
2  2   Deep  20000  9/23/2014 Operations
3  3   Aman  25000 11/15/2017         IT
4  4  Nidhi  23000  5/11/2015         HR
5  5   Sonu  30000  3/27/2019    Finance
Reference: https://stackoverflow.com/questions/2617600/importing-data-from-a-json-file-into-r

Related Topics

R Excel Files

Microsoft Excel is the spreadsheet program which stores data in the .xls and .xlsx format. R has the facility to read directly from these files using some excel specific packages....

2 minutes read.

Poisson Distribution in R Programming

Poisson distribution is a type of distribution that deals with the probability distribution of the data values by taking the mean into consideration. Poisson distribution will estimate the probability value for...

3 minutes read.

Binomial Distribution in R Programming

In this article, we will talk about the Binomial distribution in R programming. The binomial distribution is a type of probability distribution. As it is a discrete distribution, it will...

3 minutes read.

R Inheritance

One the main concept of object oriented programming is inheritance which allows us to define a new class of existing classes. This means that we can derive new classes from...

5 minutes read.

Various types of Charts in R Programming

As we know, R Language is mostly used for analyzing the data and statistics purposes to represent our data graphically & pictorially in R studio. Charts and graphs are plotted in...

4 minutes read.

R Variables, Constants and Reserved Words

R Variables Variables are very similar to an open box, where you can put any value of your wish. We can change the values as well. Variables are used to store...

4 minutes read.

R JSON File

JSON stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data. JSON file is often used when data is sent from a server to a...

2 minutes read.

Analysis of Covariance in R Programming

Analysis of Covariance can also be named ANCOVA. We know that we use the concept of regression analysis for creating models which can explain the effect of the variation in...

2 minutes read.

Linear Regression in R Programming

Linear Regression is an unsupervised machine learning algorithm in R programming. Linear Regression is a type of regression analysis that shows the relationship between two or more variables. And one...

3 minutes read.

R Basic Data Types

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are...

4 minutes read.

R If Else Statement

If statement It is the most simple decision-making statement. It checks the condition if the condition is true then only it will execute the block of statements written in the ‘if...

2 minutes read.

How to create Histograms in R

R Histograms A Histogram is the graphical representation of the distribution of numeric data. It takes only one numeric variable as input. The variable is cut into several bars (also called...

2 minutes read.

R Line Graphs

Line graph or line chart is a graph that connects a series of points by drawing segments between them. A line graph represents the relationship between 2 variables. These points...

2 minutes read.

R Operators

A symbol that tells the compiler to perform specific mathematical or logical operations is called operator. R language supports mainly 5 different types of operators, which are listed below: Arithmetic...

3 minutes read.

First R Program

First  Hello World R Program As a convention, our first R program will be the “Hello World!” program. We can run our R program either at R command prompt or we...

3 minutes read.

R Date and Time

R provides very large range of capabilities to deal with times and dates. Generally, dates are internally stored as integers, and depending on the operations we will need, we could...

3 minutes read.

Introduction to R Programming

What is R Programming? R is a programming language which provides an environment for software used for statistical analysis, graphics representation and reporting. It possesses an extensive catalog of...

4 minutes read.

How to make Boxplots in R

R Boxplots Boxplot is a measure of how well the data is distributed in a data set. It is used to give a summary of one or several numeric variables. The...

3 minutes read.

Bubble Chart in R Programming

In this article, we will talk about another type of chart in R programming, i.e., the Bubble chart. A bubble chart is a type of data visualization chart that helps display multiple...

4 minutes read.

Normal Distribution in R Programming

In statistics, the normal distribution is a type of probability function. Normal distribution tells the user about the distribution of data values in the dataset. It is a very important...

3 minutes read.