Learn JSON files in R in simple language
JSON file converts data as text to a human-readable format; Does not store. Json is actually a JavaScript object symbol. R can read JSON files using the rjson package.
Install the rjson package
In R console you can use the following command to install the rjson package:
install.packages (“rjson”)
Input data
By copying the following data into a data editor such as Windows Notepad; Create a JSON file. Save the file with a .json extension and select the file type as all files (*. *).
{
“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”]
}
Read the JSON file
JSON file by r; Reads using the JSON () function. This file is saved as a list in R.
# Load the package required to read JSON files.
library (“rjson”)
# Give the input file name to the function.
result <- fromJSON (file = “input.json”)
# Print the result.
print (result)
When we run the above code; The following result is obtained:
$ ID
[1] “1” “2” “3” “4” “5” “6” “7” “8”
$ Name
[1] “Rick” “Dan” “Michelle” “Ryan” “Gary” “Nina” “Simon” “Guru”
$ Salary
[1] “623.3” “515.2” “611” “729” “843.25” “578” “632.8” “722.5”
$ StartDate
[1] “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
[1] “IT” “Operations” “IT” “HR” “Finance” “IT”
“Operations” “Finance”
Convert JSON to a data frame
We can convert the data extracted above into a dataframe so that we can perform further analysis on it using the as.data.frame () function.
# Load the package required to read JSON files.
library (“rjson”)
# Give the input file name to the function.
result <- fromJSON (file = “input.json”)
# Convert JSON file to a data frame.
json_data_frame <- as.data.frame (result)
print (json_data_frame)
When we run the above code; The following result is obtained:
id, name, salary, start_date, dept
1 1 Rick 623.30 2012-01-01 IT
2 2 Dan 515.20 2013-09-23 Operations
3 3 Michelle 611.00 2014-11-15 IT
4 4 Ryan 729.00 2014-05-11 HR
5 NA Gary 843.25 2015-03-27 Finance
6 6 Nina 578.00 2013-05-21 IT
7 7 Simon 632.80 2013-07-30 Operations
8 8 Guru 722.50 2014-06-17 Finance