I want to retrieve data from IMF JSON RESTful Web Service API. I have found a dataset name and series code, so I submit a following request, usinng httr2 pacakge:
library(httr2)
library(tidyverse)
req <- request("http://dataservices.imf.org/REST/SDMX_JSON.svc/CompactData/IFS/PCPI_IX?startPeriod=2015&endPeriod=2020") %>%
req_perform()
As a result I get a list of 7 items.
How should I parse this output to get actual data? In the body item in the response there’re no actual numbers. I’ve tried to use fromJSON from the jsonlite library but it gives me an error (Argument ‘txt’ must be a JSON string, URL or file).
library(jsonlite)
req <- request("http://dataservices.imf.org/REST/SDMX_JSON.svc/CompactData/IFS/PCPI_IX?startPeriod=2015&endPeriod=2020") %>%
req_perform() %>%
fromJSON()
2
Answers
You can use
httr
instead:There is no need to switch to
{httr}
Source: https://httr.r-lib.org/
You can use
httr2::resp_body_json
Please see this related article.