英文:
Reading a JSON file (with 1 key to many values mapping) in R
问题
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 an 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: https://stackoverflow.com/questions/70920663/error-in-fromjsonemployee-json-not-all-data-was-parsed-0-chars-were-parse
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.
英文:
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: https://stackoverflow.com/questions/70920663/error-in-fromjsonemployee-json-not-all-data-was-parsed-0-chars-were-parse
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.
答案1
得分: 1
jsonlite::fromJSON函数似乎可以正常工作。
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论