Read JSON Data from Web APIs using R
Last Updated :
24 Apr, 2025
The Application Programming Interface allows users to use certain features like creating, reading, updating, and deleting CRUD actions without directly exposing the code. This is mostly done in the form of JavaScript Object Notation which is a text-based format for representing structured data.
Before going forward we should understand basic API calls. The CRUD operations can be performed with an API by POST, GET, PUT, and DELETE methods respectively. Of course, some methods like PUT, POST, and DELETE need authentication. In this article, we are going to see how to make an API call for getting the information using the GET method.
Take a look at this API documentation. It gives information about a word, its synonyms, etc. And if you look at this URL, it is giving us information about the word geek which is in JSON format, and looks like this:
Dictionary API to get the meaning of wordsCalling from R
To call this URL with a GET request in R Programming Language, we need a library called httr which does not come pre-installed. So go ahead and install it. To use its functionalities, include this library in the code.
R
# Installing httr
install.packages("httr")
# including the library
library(httr)
Now, let's call the URL with the GET method and store it in wordInfo:
R
# call the URL with the GET method
wordInfo <- GET("https://api.dictionaryapi.dev/api/v2/entries/en/geek")
The GET function returns a list with all the information about the server as well.
Raw data fetched via API
To get the content out of it, we need to convert this using the rawToChar function. But before that install, a library called jsonlite to work with JSON format.Â
R
# installing and loading jsonlite library
install.packages("jsonlite")
library(jsonlite)
rawToChar(wordInfo$content)
Output:
Raw data fetched via API
Now that we got our data in JSON format, to make it more clean and easy to work, we will use fromJSON function.
R
# convert to cleaner format
data <- fromJSON(rawToChar(wordInfo$content))
Output:
Using fromJSON method to get data from JSON to R
To extract the meanings,
R
Output:
Data extracted from the JSON File
It is a list, so we can use:
R
data$meanings[[1]][[2]][[1]]$definition
Output:
'A carnival performer specializing in bizarre and unappetizing behavior.'
'A person who is intensely interested in a particular field or hobby
and often having limited or nonstandard social skills. Often used with
an attributive noun.'
'(by extension) An expert in a technical field, particularly one having
to do with computers.''The subculture of geeks; an esoteric subject of
interest that is marginal to the social mainstream; the philosophy, events,
and physical artifacts of geeks; geekness.''An unfashionable or socially
undesirable person.'
You can also play with strings and do something like this:
R
library(httr)
library(jsonlite)
# storing search query in a variable
word <- "competition"
# appending the query at the end
wordInfo <- GET(paste("https://api.dictionaryapi.dev/api/v2/entries/en/", word))
data <- fromJSON(rawToChar(wordInfo$content))
data$meanings[[1]][[2]][[1]]$definition
Output:
'The action of competing.'
'A contest for a prize or award.'
'The competitors in such a contest.'
Similar Reads
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 sour
4 min read
How to Read xls files from R using gdata
Reading xls files from R using gdata is a useful way to import and manipulate data in R Programming Language. The gdata package provides a set of functions for reading and writing data in various file formats, including xls files. In this article, we will discuss the concepts related to reading xls
3 min read
Reading the CSV file into Dataframes in R
In this article, we will learn how to import or read a CSV file into a dataframe in R Programming Language. Data set in use: Step 1: Set or change the working directory In order to import or read the given CSV file into our data frame, we first need to check our current working directory, and make s
3 min read
How to Post JSON Data using Curl ?
One can send the post data using curl (Client for URLs), a command line tool, and a library for transferring data with URLs. It supports various types of protocols. Most of the use cases of the curl command are posting JSON data to a server endpoint. CURLcURL stands for ( Client for URLs) and is a c
3 min read
Building REST API using R Programming
REST(Representational state transfer) API is an architectural style that includes specific constraints for building APIs to ensure that they are consistent, efficient, and scalable. REST API is a collection of syntax and constraints which are used in the development and operation of web services tha
3 min read
Read Data Using XLSX Package in R
R is a powerful programming language used for data analysis and manipulation. The XLSX package in R is an excellent tool for reading and writing Excel files. This package allows R users to work with data stored in Excel spreadsheets directly in their R environment. In this article, we will walk you
3 min read
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. Th
4 min read
Reading Tabular Data from files in R Programming
Often, the data which is to be read and worked upon is already stored in a file but is present outside the R environment. Hence, importing data into R is a mandatory task in such circumstances. The formats which are supported by R are CSV, JSON, Excel, Text, XML, etc. The majority of times, the data
4 min read
Read JSON file using Python
The full form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called JSON. To use this feature, we import the JSON package in Pytho
4 min read
Extract all the URLs from the webpage Using R Language
In this article, we will learn how to scrap all the URLs from the webpage using the R Programming language. To scrap URLs we will be using httr and XML libraries. We will be using httr package to make HTTP requestsXML and XML to identify URLs using xml tags. httr library is used to make HTTP reque
3 min read