Home > Net >  Error in fromJSON("employee.json") : not all data was parsed (0 chars were parsed out of a
Error in fromJSON("employee.json") : not all data was parsed (0 chars were parsed out of a

Time:01-31

I was trying to read the JSON file from my r studio as a purpose of learning how to read JSON file, but suddenly i got an parsing error.


employee.json

{   
   "id" : ["1","2","3","4","5","6","7","8" ],  
   "name" : ["Shubham","Nishka","Gunjan","Sumit","Arpita","Vaishali","Anisha","Ginni" ],  
   "salary" : ["623","552","669","825","762","882","783","964"],  
   "start_date" : [ "1/1/2012","9/15/2013","11/23/2013","5/11/2014","3/27/2015","5/21/2013","7/30/2013","6/17/2014"],  
   "dept" : [ "IT","Operations","Finance","HR","Finance","IT","Operations","Finance"]  
}  

.R file

library(rjson)
emp = fromJSON("employee.json")
e = as.data.frame(emp)
print(e)

CodePudding user response:

The first argument to rjson::fromJSON is a JSON string. So your code is interpreting "employee.json" (note it has 13 characters) as JSON.

If you have saved a file named employee.json, you need to specify file = :

rjson::fromJSON(file = "employee.json")

This is not an issue when using jsonlite::fromJSON because the first argument can be a string, file or URL.

  •  Tags:  
  • Related