zap2008
zap2008

Reputation: 704

Parsing issue with facebook data in fromJSON function (R) - unexpected character error

I am trying to pull Facebook feed data from various pages to compare sentiment and I am running into trouble when converting the JSON raw text into a list object in R.

require(RCurl)
require(rjson)
access_token <- "XXXXXXXXXXXXXXXX"

FacebookScrape <-  function( path = "me", access_token, options){
  if( !missing(options) ){
    options <- sprintf( "?%s", paste( names(options), "=", unlist(options), collapse = "&", sep = "" ) )
  } else {
    options <- ""
  }
  data <- getURL( sprintf( "https://graph.facebook.com/%s%s&access_token=%s", path, options, access_token ),
                  ssl.verifypeer = FALSE)
  fromJSON(data, unexpected.escape = "skip")
}

cb.path <- "24329337724/feed?limit=300&offset=0&__after_id=354707562896&"
cb.feed <- FacebookScrape(path = cb.path, access_token = access_token)

This code returns the following Error message:

Error in fromJSON(data, unexpected.escape = "skip") : 
  unexpected character: c

I'm not very familiar with JSON, but I know that the error is occurring in the fromJSON function (line 13 in the code above). This function calls C, so using debug() doesn't tell me very much. I'm also not really sure how a simple character "c" could cause an error if the JSON text is formatted properly. It's not like "c" is an escape character or anything. I also account for escape characters with the unexpected.escape = "skip" option in fromJSON.

I have determined that the error occurs when parsing this post (there is no error if I set limit=261 in cb.path, but there is if I have limit=262). Has anyone run into a similar problem? Any help would be greatly appreciated.

Session Info:

R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] streamR_0.1        wordcloud_2.2      RColorBrewer_1.0-5 Rcpp_0.10.2        stringr_0.6.2     
 [6] plyr_1.8           tm_0.5-8.3         twitteR_1.1.6      rjson_0.2.12       ROAuth_0.9.3      
[11] digest_0.6.2       ggplot2_0.9.3.1    XML_3.95-0.1       RCurl_1.95-4.1     bitops_1.0-5      

loaded via a namespace (and not attached):
 [1] colorspace_1.2-1 dichromat_2.0-0  grid_2.15.3      gtable_0.1.2     labeling_0.1     MASS_7.3-23     
 [7] munsell_0.4      proto_0.3-10     reshape2_1.2.2   scales_0.2.3     slam_0.1-27      tools_2.15.3    

Upvotes: 2

Views: 4546

Answers (2)

JSS
JSS

Reputation: 57

I have examined your JSON

the reason is here

"message": "true\",

this caused the json in R to be parsed and become \" and a missing quote disappears .

the next line can_comment triggers the error and it starts with C

Upvotes: 0

user3580686
user3580686

Reputation: 81

I had the same issue... Base on callAPI from Rfacebook: https://github.com/pablobarbera/Rfacebook/blob/master/Rfacebook/R/utils.R use: fromJSON(rawToChar(data)

facebook <- function(url, token){
  if (class(token)=="config"){
    url.data <- GET(url, config=token)
  }
  if (class(token)=="Token2.0"){
    url.data <- GET(url, config(token=token))
  } 
  if (class(token)=="character"){
    url <- paste0(url, "&access_token=", token)
    url <- gsub(" ", "%20", url)
    url.data <- GET(url)
  }
  if (class(token)!="character" & class(token)!="config" & class(token)!="Token2.0"){
    stop("Error in access token. See help for details.")
  }
  content <- fromJSON(rawToChar(url.data$content)) # It's working very well
  if (length(content$error)>0){
    stop(content$error$message)
  } 
  return(content)
}

Call facebook function:

next.path <- "https://graph.facebook.com/29092950651/posts"
facebook( url=next.path , token)

Your access_token will active over 2hours. I use fb_oauth base on http://blog.revolutionanalytics.com/2013/11/how-to-analyze-you-facebook-friends-network-with-r.html

Best regards Robert

Upvotes: 1

Related Questions