3

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)
r2evans
  • 141,215
  • 6
  • 77
  • 149
Gourav
  • 47
  • 1
  • 8
  • This data and code do not error if I use `data.frame(jsonlite::fromJSON("employee.json"))`, perhaps it's a bug with `rjson`? (I don't have `rjson` installed, so I cannot check, sorry.) – r2evans Jan 31 '22 at 02:27

1 Answers1

6

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 = :

emp <- 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.

neilfws
  • 32,751
  • 5
  • 50
  • 63