Download JSON in R
After the JSON data is converted to a data frame, the data frame can be written into a CSV file using the write.csv() method in R. It is used to create a .csv file with the specified file name at the specified directory location. In case the data frame object is supplied, it is copied to the respective location. It can be then viewed within the local working space with ease. The write.csv() method in R has the following syntax :
Syntax: write.csv(data, path)
Arguments :
data – The data to be written in the CSV file.
path – The path where the CSV is to be stored.
R
# Importing rjson library library ( "rjson" ) # Declaring the json text json_text <- '{ "ID" :[ "1" , "2" , "3" , "4" , "5" ], "User_name" :[ "A" , "B" , "C" , "D" , "E" ], "Marks" :[34,64,24,68,76], "Branch" :[ "Commerce" , "Science" , "Humanities" , "Non-medical" , "Humanities" ] }' # Reading the json text data <- fromJSON (json_text) # Converting data into data frame data_frame <- as.data.frame (data) # Writing data into csv file write.csv (data_frame, "/Users/mallikagupta/Desktop/json_download.csv" ) |
Output:
The output of the above code is a CSV file which is stored at the location (“/Users/mallikagupta/Desktop/json_download.csv”) given in the argument.
Download and Parse JSON Using R
In this article, we are going to learn how to download and parse JSON using the R programming language.
JavaScript Object Notation is referred to as JSON. These files have the data in text form, which is readable by humans. The JSON files are open for reading and writing just like any other file. The “rjson” package must be installed in order to work with JSON files in R.
Run the below command in R to install rjson package:
install.packages("rjson")
Contact Us