Read JSON data from web APIs using R programming

Filed Under: R Programming
Read JSON Data From Web APIs Using R

Hello, folks! Today, we will be having a look at one of the interesting topics of R — How to Read JSON data from web APIS using R programming?

So, let us begin!!


What is an API?

API stands for Application Programming Interface. An API can be understood like a set or association of rules that helps us access an external service or data on web through our systems.

With respect to a programming language, an API is considered as a source of information on web that can be pulled and manipulated to certain extent through the use of some libraries.


Types of requests to an API

In association with APIs, here are some basic and important actions that one can perform when provided access to an API–

  • GET: Using this command, we can fetch the data from the APIs onto the system is JSON/XML format.
  • POST: This command helps us to add/insert data into the API structure.
  • PUT: With PUT command, we can update the existing data represented by the API.
  • DELETE: As the name suggests, it deletes the data represented by the API web service.

Steps to Pull and Read JSON data from a Web API using R

Let us now have a look at the steps to pull and read data from an API in R–

  1. Establish a healthy connection with the API.
  2. Use the GET command to pull the data.
  3. Convert the vector data into JSON format.
  4. Get the necessary values of the keys from the JSON file.

In the below example, we would be connecting to OPEN NOTIFY API service available on web. In R programming, we have made use of the below libraries to work with APIs:

  1. httr
  2. jsonlite

Further, we have used GET() command to connect to the API. After which, we have converted the information from the GET() function into a resembling JSON structure using the rawToChar() function.

Here, the JSON file contains 3 keys: [message, people, number].

We have printed the values for the keys [message and people] using json_data$key as shown below–

Example 1:

install.packages(c("httr", "jsonlite"))
rm(list = ls())
library(httr)
library(jsonlite)
info = GET("http://api.open-notify.org/astros.json")
print(info)
char_info=rawToChar(info$content)
data_json = fromJSON(char_info)

print(data_json$message)
print(data_json$people)

Output:

Here, the response token ‘200’ suggests that the connection is healthy.

> print(info)
Response [http://api.open-notify.org/astros.json]
  Date: 2021-01-23 07:37
  Status: 200
  Content-Type: application/json
  Size: 356 B

> print(data_json$message)
[1] "success"
> print(data_json$people)
  craft                 name
1   ISS      Sergey Ryzhikov
2   ISS          Kate Rubins
3   ISS Sergey Kud-Sverchkov
4   ISS         Mike Hopkins
5   ISS        Victor Glover
6   ISS       Shannon Walker
7   ISS       Soichi Noguchi

Example 2:

Now, let us form a connection to one of the Gmail APIs and print the content.

Having made a connection, we use names() function to get the keys of the JSON data file. After which, we print the values of the various keys offered in the JSON file of the API as shown below:

install.packages(c("httr", "jsonlite"))
rm(list = ls())
library(httr)
library(jsonlite)

info = GET("https://gmail.googleapis.com/$discovery/rest?version=v1")
print(info)

char_info=rawToChar(info$content)

data_json = fromJSON(char_info)
names(data_json)
print(data_json$ownerName)
print(data_json$description)

Output:

> print(info)
Response [https://gmail.googleapis.com/$discovery/rest?version=v1]
  Date: 2021-01-23 07:32
  Status: 200
  Content-Type: application/json; charset=UTF-8
  Size: 163 kB
{
  "version": "v1",
  "servicePath": "",
  "auth": {
    "oauth2": {
      "scopes": {
        "https://www.googleapis.com/auth/gmail.metadata": {
          "description": "View your email message metadata such as labels and headers, but not the email body"
        },
        "https://www.googleapis.com/auth/gmail.addons.current.message.action": {
...

> names(data_json)
 [1] "version"           "servicePath"       "auth"              "description"       "ownerName"        
 [6] "title"             "icons"             "documentationLink" "mtlsRootUrl"       "basePath"         
[11] "rootUrl"           "kind"              "id"                "parameters"        "baseUrl"          
[16] "protocol"          "resources"         "name"              "revision"          "ownerDomain"      
[21] "schemas"           "discoveryVersion"  "batchPath"         "canonicalName"    

> print(data_json$ownerName)
[1] "Google"
> print(data_json$description)
[1] "The Gmail API lets you view and manage Gmail mailbox data like threads, messages, and labels."

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned and till then, Happy Learning!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content