Web Scraping Data from JSON

We are going to scrap this JSON data about COVID cases available here, which will look something like this:

Raw JSON data

To get this data in R, we need a library called jsonlite. This doesn’t come pre-installed in R. So, install it. It is also necessary to include it in the script.

R




# installing the library
install.packages("jsonlite")
  
# include it
library(jsonlite)


Now that we have loaded the library, we can use fromJSON function to parse the data. Pass the same URL given above about the COVID data to the function where raw data is a large list containing the information.

R




# scraping the JSON data to R
rawdata <- fromJSON("https://data.covid19india.org/v4/min/timeseries.min.json")


Accessing the data

Let us now try to access some information. If we simply output rawdata, we get this long list which contains the data of COVID cases across all states and union territories in India:

 

 If we want to get the data only for Delhi that is DL, we can look into the JSON format on the webpage and try this:

R




# getting the information for DL (Delhi)
data <- rawdata['DL']


which outputs all the information about Delhi:

Data

If we want information about a specific date, we can do so by accessing data for the date 2020-03-16. As you can see in JSON:

R




# data for specific date
data[[1]][[1]][[15]]


Output:

Data for DL on 16 March 2020

You can go ahead and perform various actions on this list like analyzing the data, plotting graphs, and much more! See this tutorial on R lists, to try such actions.

Web Scraping R Data From JSON

Many websites provide their data in JSON format to be used. This data can be used by us for analysis in R.  JSON (JavaScript Object Notation) is a text-based format for representing structured data based on JavaScript object syntax. In this article, we will see how to scrap data from a JSON web source and use it in R Programming Language.

Similar Reads

Web Scraping Data from JSON

We are going to scrap this JSON data about COVID cases available here, which will look something like this:...

Web Scraping using XPath

...