Search code examples
jsonrgoogle-custom-search

Error importing json data using google custom search and R


I am using google custom search with R. The code looks like,

library(httr,lib.loc = "C:\\R packages")
URL<-"https://www.googleapis.com/customsearch/v1?"
key<-"key=xxxxxxxxxxxxxxxxxxxxxxxexxx"
cx<-"&cx=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
q<-"&q="
prod<-gsub(" ","+",prod)
URL<-paste0(URL,key,cx,q,prod)

I am able to get the json output when I copy paste the URL in my browser. However,

 content(GET(URL))

gives the following error,

Error in curl::curl_fetch_memory(url, handle = handle) : 
Problem with the SSL CA cert (path? access rights?)

Any input would be great. Thanks.


Solution

  • You can read JSON directly from a url using library(jsonlite) and the function fromJSON()

    library(jsonlite)
    js <- fromJSON(url)
    

    Where ?fromJSON gives

    fromJSON(txt, simplifyVector = TRUE, simplifyDataFrame = simplifyVector, simplifyMatrix = simplifyVector, flatten = FALSE, ...)

    txt - a JSON string, URL or file