MikeR MikeR - 4 months ago 21
JSON Question

Retrieving data in R from Import.io API using Rcurl?

Im trying to retrieve data from an import.io 'connector' API.

Basically, i've trained the extractor to the structure of a given website, and I want to import the data from within R using this approach:
1) Retrieve the Json results from the API
2) Save each query-result into a given dataframe

My plan was to use RCurl for querying the API-link:
https://api.import.io/store/connector/9128b4e0-9ae2-4232-b202-c1e8766ed01f/_query?input=webpage/url:[ENCODED URL]&&_apikey=[API-KEY]

require(Rcurl)
Raw.Data <- curl::curl(url = "https://api.import.io/store/connector/9128b4e0-9ae2-4232-b202-c1e8766ed01f/_query?input=webpage/url:[ENCODED URL]&&_apikey=[API-KEY]")


And after that, use Rjson for reading the retrieved data into a dataframe:

require(rjson)
FromJson_To_DataFrame <-(Raw.data)


There is something missing because I am getting errors, but i cannot figure out what it is, and if it is possible at all like this. Hints would be greatly aprecaiated!

Cheers from DK

Answer

I found an answer to this problem, which is fairly straightforward. The json object retrieved from the import.io API can be changed in to coulums in a DF by accesing it via $:

library(httr)   
output <- get(https://api.import.io/store/connector/9128b4e0-9ae2-4232-b202-c1e8766ed01f/_query?input=webpage/url:[ENCODED URL]&&_apikey=[API-KEY])

result <- content(output)

vector1 <- result$results$variable1
vector2 <- result$results$variable2

and then you can cbind them to a dataframe if you need to.

Comments