如何在R中逐行读取json文件?

时间:2019-03-12 11:07:13

标签: r json dataframe

以下是原始的json数据:

json_file <-  '{"name":"Doe, John","group":"Red","age":{"v_0":24}}
    {"name":"Doe, Jane","group":"Green","age":{"v_0":31}}
    {"name":"Smith, Joan","group":"Yellow","age":{"v_0":22}}'

当我要将json_file转换为数据框时:

library(RJSONIO)
json_file <- fromJSON(json_file)

我收到此错误:

Error: parse error: trailing garbage
      :"Red","age":{"v_0":24}}     {"name":"Doe, Jane","group":"Gr
                 (right here) ------^

我知道如果我将原始数据更改为以下数据,一切都会很好:

json_file <-  '[{"name":"Doe, John","group":"Red","age":{"v_0":24}},
    {"name":"Doe, Jane","group":"Green","age":{"v_0":31}},
    {"name":"Smith, Joan","group":"Yellow","age":{"v_0":22}}]'

但实际上我想知道:

1)如何使用[,]从原始数据中获取数据帧而不拆分其对象?

2)如果没有办法,如何通过将json添加到除最后一行之外的每一行的末尾,并添加,来拆分大型[文件中的对象]到文件的第一行和最后一行?

2 个答案:

答案 0 :(得分:0)

有多种方法可以执行此操作,而无需编辑文件。

如果要使用data.frame:

library(jsonlite)
# url
zips <- stream_in(url("http://media.mongodb.org/zips.json"))
# file
json_data <- stream_in(file("path/to/file.json"))

或者如果您想要列表:

json_data_as_list <- readLines("path/to/file.json") %>% lapply(fromJSON)

答案 1 :(得分:-1)

您需要那些方括号。将以下内容另存为“ test.json”:

{ 
   "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"]
}

现在,加载所需的库并指向您刚刚保存的文件:

# Load the package required to read JSON files.
library("rjson")

# Give the input file name to the function.
result <- fromJSON(file = "C:\\Users\\Excel\\Documents\\test.json")

# Print the result.
print(result)

结果:

print(result)
$ID
[1] "1" "2" "3" "4" "5" "6" "7" "8"

$Name
[1] "Rick"     "Dan"      "Michelle" "Ryan"     "Gary"     "Nina"     "Simon"    "Guru"    

$Salary
[1] "623.3"  "515.2"  "611"    "729"    "843.25" "578"    "632.8"  "722.5" 

$StartDate
[1] "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
[1] "IT"         "Operations" "IT"         "HR"         "Finance"    "IT"         "Operations" "Finance"