2

I have a file named data.json. It has the following contents:

{ 
   "ID":["1","2","3","4","5","6","7","8" ],
   "Name":["Rick","Dan","Michelle","Ryan","Gary","Nina","Simon","Guru" ],
   "Salary":["623.3","515.2","611","729","843.25","578","632.8","722.5" ],
   
   "StartDate":[ "1/1/2012","9/23/2013","11/15/2014","5/11/2014","3/27/2015","5/21/2013",
      "7/30/2013","6/17/2014"],
   "Dept":[ "IT","Operations","IT","HR","Finance","IT","Operations","Finance"]
}

In RStudio, I have installed the 'rjson' package and have the following code:

library("rjson")
myData <- fromJSON(file="data.json")
print(myData)

As per the description of the fromJSON() function, it should read the contents of 'data.json' file into a R object 'myData'. When I executed it, I got the following error:

Error in fromJSON(file = "data.json") : 
not all data was parsed (0 chars were parsed out of a total of 3 chars)

I validated the structure of the 'data.json' file on https://jsonlint.com/. It was valid.

I searched stackoverflow.com and got the following page: Error in fromJSON("employee.json") : not all data was parsed (0 chars were parsed out of a total of 13 chars)

My program already complies with the answers given here but the 'data.json' file is still not getting parsed.

I would be grateful if you could point out what mistake I am making in the R program or JSON file as I am new to both.

Thank You.

Mahesh
  • 23
  • 4

1 Answers1

1

I can confirm the error for rjson, but jsonlite::fromJSON appears to work.

jsonlite::fromJSON('foo.dat') |> as.data.frame()
#   ID     Name Salary  StartDate       Dept
# 1  1     Rick  623.3   1/1/2012         IT
# 2  2      Dan  515.2  9/23/2013 Operations
# 3  3 Michelle    611 11/15/2014         IT
# 4  4     Ryan    729  5/11/2014         HR
# 5  5     Gary 843.25  3/27/2015    Finance
# 6  6     Nina    578  5/21/2013         IT
# 7  7    Simon  632.8  7/30/2013 Operations
# 8  8     Guru  722.5  6/17/2014    Finance
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thanks for the answer. When I tried it, I got the following error. "Error: lexical error: invalid char in json text. { (right here) ------^ In addition: There were 14 warnings (use warnings() to see them)." My JSON file is perfectly valid. I am unable to figure out why I am getting the error. – Mahesh Feb 20 '23 at 08:28