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

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

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:

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

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

  1. library("rjson")
  2. myData <- fromJSON(file="data.json")
  3. 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:

  1. Error in fromJSON(file = "data.json") :
  2. 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:

  1. {
  2. &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; ],
  3. &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; ],
  4. &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; ],
  5. &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;,
  6. &quot;7/30/2013&quot;,&quot;6/17/2014&quot;],
  7. &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;]
  8. }

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

  1. library(&quot;rjson&quot;)
  2. myData &lt;- fromJSON(file=&quot;data.json&quot;)
  3. 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:

  1. Error in fromJSON(file = &quot;data.json&quot;) :
  2. 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函数似乎可以正常工作。

  1. jsonlite::fromJSON('foo.dat') |&gt; as.data.frame()
  2. # ID Name Salary StartDate Dept
  3. # 1 1 Rick 623.3 1/1/2012 IT
  4. # 2 2 Dan 515.2 9/23/2013 Operations
  5. # 3 3 Michelle 611 11/15/2014 IT
  6. # 4 4 Ryan 729 5/11/2014 HR
  7. # 5 5 Gary 843.25 3/27/2015 Finance
  8. # 6 6 Nina 578 5/21/2013 IT
  9. # 7 7 Simon 632.8 7/30/2013 Operations
  10. # 8 8 Guru 722.5 6/17/2014 Finance
英文:

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

  1. jsonlite::fromJSON(&#39;foo.dat&#39;) |&gt; as.data.frame()
  2. # ID Name Salary StartDate Dept
  3. # 1 1 Rick 623.3 1/1/2012 IT
  4. # 2 2 Dan 515.2 9/23/2013 Operations
  5. # 3 3 Michelle 611 11/15/2014 IT
  6. # 4 4 Ryan 729 5/11/2014 HR
  7. # 5 5 Gary 843.25 3/27/2015 Finance
  8. # 6 6 Nina 578 5/21/2013 IT
  9. # 7 7 Simon 632.8 7/30/2013 Operations
  10. # 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:

确定