在R中读取一个JSON文件(具有1个键到多个值的映射):

huangapple go评论81阅读模式
英文:

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:

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

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

library(&quot;rjson&quot;)
myData &lt;- fromJSON(file=&quot;data.json&quot;)
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 = &quot;data.json&quot;) : 
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') |&gt; 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(&#39;foo.dat&#39;) |&gt; 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

huangapple
  • 本文由 发表于 2023年2月19日 01:33:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定