Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected character json error in r

Tags:

json

r

I'm getting the following error message for this code intended to read Instagram posts into r. Other code is successfully reading in the first 20 posts, but Instagram wants you to request the "next_url" to get additional blocks of posts. I looked at similar answers to this error, but can't find the typo in my code. Did wonder if it could be something coming from the API that needs to be adjusted. I'm new to R and hope I explained this appropriately. I would greatly appreciate any assistance.

Error in fromJSON(getURL(paste(next_url, token, sep = "")), unexpected.escape = "keep") : 
  unexpected character: p
In addition: Warning message:
In fromJSON(getURL(paste(next_url, token, sep = "")), unexpected.escape = "keep") :
  unexpected escaped character '\ ' at pos 26. Keeping value.
  username <- "XXXX"
    if(grepl(received_profile$username,username))
  {
    user_id <- received_profile$id
    media <- fromJSON(getURL(paste(next_url,token,sep="")),unexpected.escape = "keep")
    df1 = data.frame(no = 1:length(media$data))

    for(i in 1:length(media$data))
    {
      #comments
      df1$comments[i] <-media$data[[i]]$comments$count

      #likes:
      df1$likes[i] <- media$data[[i]]$likes$count

      #date
      df1$date[i] <- toString(as.POSIXct(as.numeric(media$data[[i]]$created_time), origin="1970-01-01"))
    }
    df <- rbind(df, df1)    
  }else
  {
    print("Error: User not found!")
  }
like image 238
Steve F Avatar asked Sep 25 '15 19:09

Steve F


1 Answers

I can reproduce your problem by extending your code snippet with code that extracts next_url from the pagination object in the JSON response returned by Instagram.

When using rjson::fromJSON to parse the JSON response I get errors like

Error in rjson::fromJSON(js) : unexpected character: i

However, using RJSONIO::fromJSON for parsing the exact same string works just fine! Also the string validates as correct JSON with online JSON validators.

This strongly points to a bug in rjson::fromJSON. You may want to use the RJSONIO package instead for now.

Unfortunately these Instagram replies are quite large. I'll see if I can strip down the JSON output to something manageable so I can post details here. (I'm trying to avoid having to link to data on an external site.)

I suspect an encoding issue with Instagram user names.

like image 81
WhiteViking Avatar answered Sep 20 '22 03:09

WhiteViking