How to Read/Write Data from JSON Files using R Programming

JSON stands for JavaScript Object Notation and it is a very popular syntax for storing and exchanging data. With the ease of operation JSON has become an simpler alternative of XML. I have already shown How to Parse/Read XML files using R Programming. Now in this tutorial I will show How to Read JSON Files in R Programming. For this class I am using the following JSON file:

{“employees”:[
    {“firstName”:”Achin”, “lastName”:”Jain”},
    {“firstName”:”Manish”, “lastName”:”Kumar”},
    {“firstName”:”Arun”, “lastName”:”Dubey”}
]}

Read Also:

How to Read JSON Files in R Programming

To read JSON File you need to install and load “rjson” package. To do so use the following commands:

> install.packages(“rjson”)

> library(“rjson”)

To demonstrate How to Read JSON Files I have created a “emp.json” file using the above mentioned code. Now to Read use fromJSON() function as shown below:

How to Read JSON Files in R Programming

How to Write Data to JSON File using R Programming

To write data use “toJSON()” function. See the image below to see how to write content in JSON file.

How to Write Data to JSON File using R Programming

How to Convert JSON to Data Frame

Like in the previous example we have seen How to Convert XML file into Data Frame. Similarly we can convert JSON into Data Frame using “as.data.frame()” function. For example I am converting “emp.json” file to dataframe.

How to Convert JSON to Data Frame

Hope you liked the article, Keep Reading Technokarak.com for more tutorials on R Programming.

Comment below if you face problem while practicing the above Tutorial on How to Parse Website Source Code.

Leave a Reply