Open In App
Related Articles

Working with JSON Files in R Programming

Like Article
Save Article
Report issue

JSON stands for JavaScript Object Notation. These files contain the data in human readable format, i.e. as text. Like any other file, one can read as well as write into the JSON files. In order to work with JSON files in R, one needs to install the “rjson”  package. The most common tasks done using JSON files under rjson packages are as follows:

  • Install and load the rjson package in R console
  • Create a JSON file
  • Reading data from JSON file
  • Write into JSON file
  • Converting the JSON data into Dataframes
  • Working with URLs 

Install and load the rjson package

One can install the rjson from the R console using the install.packages() command in the following way:


After installing rjson package one has to load the package using the library() function as follows:


Creating a JSON file 

To create a JSON file, one can do the following steps:

  • Copy the data given below into a notepad file or any text editor file. One can also create his own data as per the given format.
  • Choose “all types” as the file type and save the file with .json extension.(Example: example.json)
  • One must make sure that the information or data is contained within a pair or curly braces { } .

Reading a JSON file

In R, reading a JSON file is quite a simple task. One can extract and read the data of a JSON file very efficiently using the fromJSON() function. The fromJSON() function takes the JSON file and returns the extracted data from the JSON file in the list format by default.


Suppose the above data is stored in a file named example.json in the E drive. To read the file we must write the following code.

# Read a JSON file
# Load the package required to read JSON files.
# Give the input file name to the function.
result <- fromJSON(file = "E:\\example.json")
# Print the result.



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

[1] "Mithuna"   "Tanushree" "Parnasha"  "Arjun"     "Pankaj"

[1] "722.5"  "815.2"  "1611"   "2829"   "843.25"

[1] "6/17/2014"  "1/1/2012"   "11/15/2014" "9/23/2013"  "5/21/2013"

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

Writing into a JSON file 

One need to create a JSON Object using toJSON() function before he writes the data to a JSON file. To write into a JSON file use the write() function.


# Writing into JSON file.
# Load the package required to read JSON files.
# creating the list
list1 <- vector(mode="list", length=2)
list1[[1]] <- c("sunflower", "guava", "hibiscus")
list1[[2]] <- c("flower", "fruit", "flower")
# creating the data for JSON file
jsonData <- toJSON(list1)
# writing into JSON file
write(jsonData, "result.json"
# Give the created file name to the function
result <- fromJSON(file = "result.json")
# Print the result



[1] "sunflower" "guava"     "hibiscus"

[1] "flower" "fruit"  "flower"

Converting the JSON data into Dataframes

In R, to convert the data extracted from a JSON file into a data frame one can use the function. 


# Convert the file into dataframe
# Load the package required to read JSON files.
# Give the input file name to the function.
result <- fromJSON(file = "E://example.json")
# Convert JSON file to a data frame.
json_data_frame <-



  ID      Name Salary  StartDate       Dept
1  1   Mithuna  722.5  6/17/2014         IT
2  2 Tanushree  815.2   1/1/2012         IT
3  3  Parnasha   1611 11/15/2014         HR
4  4     Arjun   2829  9/23/2013 Operations
5  5    Pankaj 843.25  5/21/2013    Finance

Working with URLs 

One can take datasets from any websites and extract the data and use them. This can be done under any of two packages, namely RJSONIO and jsonlite.


# working with URLs
# import required library
# extracting data from the website
Raw <- fromJSON(
# extract the data node
food_market <- Raw[['data']]
# assembling the data into data frames
Names <- sapply(food_market, function(x) x[[14]])



[1] "LUCKY MART             " "CUMBERLAND FARMS 1587  "
[3] "K&M SPORTS             " "MASON&OLD RIDGE FARM   "

Last Updated : 30 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Similar Reads