Saturday, November 28, 2020

How to Use JSON File in R?

 JSON stands for JavaScript Object Notation. The JSON file contains the data as text in a human-readable format. Like other files, we can also read and write into the JSON files. For this purpose, R provides a package named rjson, which we have to install with the help of the familiar command install.packages.

R JSON File

Install rjson package

By running the following command into the R console, we will install the rjson package into our current working directory.

  1. install.packages("rjson")  

Output


Creating a JSON file

The extension of JSON file is .json. To create the JSON file, we will save the following data as employee_info.json. We can write the information of employees in any text editor with its appropriate rule of writing the JSON file. In JSON files, the information contains in between the curly braces({}).

Example: employee_info.json

  1. {   
  2.    "id":["1","2","3","4","5","6","7","8" ],  
  3.    "name":["Shubham","Nishka","Gunjan","Sumit","Arpita","Vaishali","Anisha","Ginni" ],  
  4.    "salary":["623","552","669","825","762","882","783","964"],  
  5.   
  6.    "start_date":[ "1/1/2012","9/15/2013","11/23/2013","5/11/2014","3/27/2015","5/21/2013",  
  7.       "7/30/2013","6/17/2014"],  
  8.    "dept":[ "IT","Operations","Finance","HR","Finance","IT","Operations","Finance"]  
  9. }  

Output

R JSON File

Read the JSON file

Reading the JSON file in R is a very easy and effective process. R provide from JSON() function to extract data from a JSON file. This function, by default, extracts the data in the form of a list. This function takes the JSON file and returns the records which are contained in it.

Let's see an example to understand how fromJSON() function is used to extract data and print the result in the form of a list. We will consider the employee_info.json file which we have created before.

Example

  1. # Loading the package which is required to read JSON files.  
  2. library("rjson")  
  3.   
  4. # Giving the input file name to the function fromJSON.  
  5. result <- fromJSON(file = "employee_info.json")  
  6.   
  7. # Printing the result.  
  8. print(result)  

Output

R JSON File

Converting JSON data to a Data Frame

R provide, as.data.frame() function to convert the extracted data into data frame. For further analysis, data analysts use this function. Let's start an example to see how this function is used, and in our example, we will consider our employee_info.json file.

Example

  1. # Loading the package which is required to read JSON files.  
  2. library("rjson")  
  3.   
  4. # Giving the input file name to the function fromJSON.  
  5. result <- fromJSON(file = "employee_info.json")  
  6.   
  7. # Converting the JSON record to a data frame.  
  8. data_frame <- as.data.frame(result)  
  9.   
  10. #Printing JSON data frame  
  11. print(data_frame)  

Output

R JSON File


No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...