3

I am trying to convert this json object to a data frame. I am getting an error that character '0' is not recognized. Any suggestion on how to handle this error occurring in a large json file having 1000 records?

library(RJSONIO)

json_file<- '{ "_id" : ObjectId( "539163d7bd350003" ), "login" :    "vui", "id" : 369607, "avatar_url" : "https://avatars.mashupsusercontent.com/u/369607?", "gravatar_id" : "df8897ffebe16c5b0cd690925c63e190", "url" : "https://api.mashups.com/users/vui", "html_url" : "https://mashups.com/vui", "followers_url" : "https://api.mashups.com/users/vui/followers", "following_url" : "https://api.mashups.com/users/vui/following{/other_user}", "gists_url" : "https://api.mashups.com/users/vui/gists{/gist_id}", "starred_url" : "https://api.mashups.com/users/vui/starred{/owner}{/repo}", "subscriptions_url" : "https://api.mashups.com/users/vui/subscriptions", "organizations_url" : "https://api.mashups.com/users/vui/orgs", "repos_url" : "https://api.mashups.com/users/vui/repos", "events_url" : "https://api.mashups.com/users/vui/events{/privacy}", "received_events_url" : "https://api.mashups.com/users/vui/received_events", "type" : "User", "site_admin" : false, "org" : "amurath" }'

json_file <- fromJSON(json_file)

json_file <- lapply(json_file, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})

df<- do.call("rbind", json_file)

### Error in fromJSON(json_file) : unexpected character 'O'
user3570187
  • 1,743
  • 3
  • 17
  • 34
  • 3
    That JSON does not validate: http://jsonlint.com/. It does not appear to be a valid JSON format. – MrFlick Jun 11 '15 at 17:29
  • Great! Looks like the first column object id is causing issue. Is there a way to remove all these elements "_id" : ObjectId( "539163d7bd350003" ), which are invalidating the json object from the file? – user3570187 Jun 11 '15 at 18:20
  • There are a million different reasons you could have an invalid JSON file and it's not obvious how to "fix" or "remove" them properly. You really should go back to the code that's generating the invalid JSON and figure out how to make it produce valid data. Trying to fix it after the fact is a nightmare unless you are willing to make very string assumptions about how the data needs to be fixed. – MrFlick Jun 11 '15 at 18:23
  • I checked it as per jsonlint.com and I see that the first object is a combination of string and number which is causing the issue. I removed this column and i was able to make a data frame. But i have over 1000 rows or more so i wondering how to make it work for multiple records! – user3570187 Jun 11 '15 at 18:27

0 Answers0