I have written the following code in R to start using a data request API. It's a normal web service JSON API.
library(RJSONIO)
library(RCurl)
library(httr)
r <- POST("http://api.scb.se/OV0104/v1/doris/sv/ssd/START/PR/PR0101/PR0101A/KPIFastM2",
body = '{ "query": [], "response": { "format": "json" } }')
stop_for_status(r)
a<-content(r, "text", "application/json", encoding="UTF-8")
cat(a, file = "test.json")
x<-fromJSON(file("test.json", "r"))
mydf<-do.call(rbind, lapply(x$data, data.frame))
colnames(mydf)<-c("YearMonth", "CPI")
Basically it initialized a get reuest for the URL using httr and then convert the resulting JSON data to an R structure via fromJSON. The JSON request looks like this:
{ "query": [], "response": { "format": "json" } }
Indeed my code gets the data into a data.frame like I wanted it to, but it is painfully verbose and I refuse to believe that all of these lines are necessary to achieve the wanted result. The wanted result is the mydf data.frame of course.
So to my question: What is the shortest and most correct way to get the data from the web service into the data.frame?
Cheers, Michael