1

I recently created a function that is calling an API and gives the following results (json format):

{"data":{"loc":"google.fr","lang":"fr","domain":"domain.com","result":[["voyage thailande",1,"https://www.blabla.com/thailande/"]]}}

My purpose is to get a data.frame with "only" the last part of the after the "result":

voyage thailande, 0, https://www.blabla.com/thailande/

Right now I only succeeded to get the following data.frame (see the picture):enter image description here

With the following code:

library(rjson)
keyword_checker <- function(keyword, domain, loc, lang){

  keyword_to_check <- as.character(keyword)

  api_request <- paste("https://script.fromgoogle.....",
                       "?kw=",keyword,
                       "&domain=",domain,
                       "&loc=",loc,
                       "&lang=",lang,sep="")
  api_request <- URLencode(api_request, repeated = TRUE)  
  source <-fromJSON(file = api_request)#Convertir un Json file en Data Frame
  return(data.frame(do.call("rbind", source)))
}

Thank you for your help

Franck
  • 87
  • 8
  • 1
    `source$data$result` will return the requested information. – Dave2e Mar 12 '19 at 16:38
  • thank you! I just tested it with `source$result` that's why it wasn't working – Franck Mar 12 '19 at 16:44
  • So, as I said, this is working fine now, but I have another problem... In my data, instead of a vector, I have a list. how is that possible considering that I used the `data.frame`? – Franck Mar 13 '19 at 13:34
  • `fromJson` function is returning a hierarchical list of lists. The data.frame function is just packing the sub-lists into the cells of the data frame so you many need to `unlist` function to finish cleaning the data. I suggest, asking another question providing a larger sample size and a better explanation of the desired result. – Dave2e Mar 13 '19 at 13:44

0 Answers0