JSON Files in R: Reading and Writing (2019)

Introduction to JSON Files in R

A JSON file stores simple data structures and objects in JavaScript Object Notation (JSON) format. JSON is a standard data lightweight interchange format primarily used for transmitting data between a web application and a server. The JSON file is a text file that is language-independent, self-describing, and easy to understand. In this article, we will discuss reading and writing a JSON file in R Language in detail using the R package “rjson“.

Since JSON file format is text only, it can be sent to and from a server and used as a data format by any programming language. The data in the JSON file is nested and hierarchical. Let us start reading and writing JSON files in R.

Creating JSON File

Let’s create a JSON file. Copy the following lines into a text editor such as Notepad. Save the file with a .json extension and choose the file type as all files(*.*). Let the file name be “data.json”, stored on the “D:” drive.

{ 
"ID":["1","2","3","4","5","6","7","8" ],
"Name":["Rick","Dan","Michelle","Ryan","Gary","Nina","Simon","Guru" ],
"Salary":["623.3","515.2","611","729","843.25","578","632.8","722.5" ],
"StartDate":[ "1/1/2012","9/23/2013","11/15/2014","5/11/2014","3/27/2015","5/21/2013",
"7/30/2013","6/17/2014"],
"Dept":[ "IT","Operations","IT","HR","Finance","IT","Operations","Finance"]
}
Reading and Writing JSON files in R

Installing rjson R Package

The R language can also read the JSON files using the rjson package. To read a JSON data file, First, install the rjson package. Issue the following command in the R console, to install the rjson package.

install.packages("rjson")

The rjson package needs to be loaded after installation of the package.

Reading JSON Files in R

To read a JSON file, the rjson package needs to be loaded. Use the fromJSON( ) function to read the file.

# Give the data file name to the function.
result <- fromJSON(file = "D:\\data.json")
# Print the result.
print(result)

The JSON file now can be converted to a Data Frame for further analysis using the as.data.frame() function.

# Convert JSON file to a data frame.
json_data_frame <- as.data.frame(result)
print(json_data_frame)

Writing JSON objects to .Json file

To write JSON Object to file, the toJSON() function from the rjson library can be used to prepare a JSON object and then use the write() function for writing the JSON object to a local file.

Let’s create a list of objects as follows

list1 <- vector(mode="list", length=2)
list1[[1]] <- c("apple", "banana", "rose")
list1[[2]] <- c("fruit", "fruit", "flower")

read the above list to JSON

jsonData <- toJSON(list1)

write JSON object to a file

write(jsonData, "output.json")

Read more about importing and exporting data in R: see the post

MCQs General Knowledge

Input Data in R Language: c() & scan() Function

Introduction to Input Data in R Language

There are many ways to input data in R Language. Here, I will concentrate only on typing data directly at the keyboard using c() and scan() functions, which are very common ways to input data in R language.

Traditional statistical computer software such as Minitab, SPSS, and SAS, etc., are designed to transform rectangular datasets (a dataset whose rows represent the observations and columns represent the variables) into printed reports and graphs. However, R and S languages are designed to transform data objects into other data objects (such as reports, and graphs).

S and R language both support rectangular datasets, in the form of data frames and other data structures. Here we will learn to know about data in R to work efficiently as a statistical data analyst.

Data Input Functions in R

There are many ways to input data in R and S-Plus. Let us learn to type data directly on the keyboard.

Input Data Using c() Function

The best choice is to enter small datasets directly on the keyboard. Remember that it is impractical to enter a large data set directly at the keyboard.

Let us use the c() function to enter the vector of numbers directly as:

x    <- c(1, 3, 5, 7, 9)
char <- c('a', 'b', 'c', 'd')
TF   <- c(TRUE, FALSE)
Input Data in R Language

Note that the character strings can be directly inputted in single or double quotation marks. For example, "a" and 'a' both are equivalent.

Input Data Using Scan() Function in R

It is also very convenient to use the scan() function in R, which prompts with the index of the next entry.  Consider the example,

xyz <- scan()
1: 10 20 30 35
5: 40 35 25
8: 9 100 50
11:
Read 10 items

The number before the colon on each of the inputted lines is the index of the next data entry point (observation) to be entered. Note that entering a blank line terminates the scan() function input behavior.

Click the following links to learn about data entry (import and export internal and external data) in R Language

R FAQS https://rfaqs.com

Online MCQs Test Preparation Website with Answers

Best R Language Test 3

This quiz “R Language Test” will help you to check your ability to execute some basic operations on objects in the R language, and it will also help you to understand some basic concepts. This quiz about R Language Test will help you to improve your computational understanding. Let us start with R Language Test Quiz.

Online MCQs about R Programming Language with Answers

1. The ________ function takes a vector or other objects and splits it into groups determined by a factor or list of factors.

 
 
 
 

2. R comes with a ________ to help you optimize your code and improve its performance.

 
 
 
 

3. What would be the output of the following code?

> x <- 1:4
> y <- 6:9
> z <- x + y
> z

 
 
 
 

4. R functionality is divided into a number of

 
 
 
 

5. ______ function is same as lapply( )

 
 
 
 

6. Which of the following is used for Statistical analysis in R language?

 
 
 
 

7. An analyst is checking the value of the variable x using a logical operator, so they run the following code:

x > 35 & x < 65

Which values of x would return TRUE when the analyst runs the code? Select all that apply.

 
 
 
 

8. What would be the value of the following expression?

> log(-1)

 
 
 
 

9. What would be the output of the following code?
> x <- 1:4
> x > 2

 
 
 
 

10. When working in R, for which part of the data analysis process do analysts use the tidyr package?

 
 
 
 

11. The debug( ) flags a function for ______ mode in R mode.

 
 
 
 

12. Which of the following is a base package for the R language?

 
 
 
 

13. _______ loop over a list and evaluate a function on each element

 
 
 
 

14. _______ applies a function over the margins of an array

 
 
 
 

15. Which of the following is an example of a vectorized operation for subtraction operation

> x<- 1:4
> y<- 6:9

 
 
 
 

16. The lapply( ) function takes _______ arguments

 
 
 
 
 

17. A matrix is _______ dimensional rectangular data set.

 
 
 
 

18. Why would a data analyst want to use the CRAN network when working with RStudio?

 
 
 
 

19. ____ ____is used to apply a function over subsets of a vector

 
 
 
 

20. Which of the following method make a vector of repeated values?

 
 
 
 

The R language Test covers some looping functions such asapply(), lapply(), mapply(), sapply(), and tapply(). Results from the execution of r codes are also asked.

rfaqs.com R Language Test

Online R Language Test

  • Which of the following is a base package for the R language?
  • R comes with a to help you optimize your code and improve its performance.
  • The debug( ) flags a function for mode in R mode.
  • A matrix is _________ a dimensional rectangular data set.
  • The function takes a vector or other objects and splits it into groups determined by a factor or list of factors.
  • The lapply( ) function takes arguments
  • ________is used to apply a function over subsets of a vector
  • ________ applies a function over the margins of an array
  • _________ function is the same as lapply( ) __________ loop over a list and evaluate a function on each element
  • Which of the following methods makes a vector of repeated values?
  • Which of the following is used for Statistical analysis in R language?
  • R functionality is divided into a number of
  • Which of the following is an example of a vectorized operation for subtraction operation > x<- 1:4 > y<- 6:9 What would be the output of the following code? > x <- 1:4 > y <- 6:9 > z <- x + y > z
  • What would be the output of the following code? > x <- 1:4 > x > 2
  • What would be the value of the following expression? > log(-1)
  • “An analyst is checking the value of the variable x using a logical operator, so they run the following code:x > 35 & x < 65
    Which values of x would return TRUE when the analyst runs the code? Select all that apply.”
  • When working in R, for which part of the data analysis process do analysts use the tidyr package?
  • Why would a data analyst want to use the CRAN network when working with RStudio?

R Programming Language

Computer MCQs Test Online