[英]Merging multiple JSON files in R
I am currently trying to merge multiple (380) JSON files into one dataframe. 我目前正在尝试将多个(380)JSON文件合并到一个数据帧中。
I came up with the following code, which I think should work. 我提出了以下代码,我认为应该可行。 But unfortunately I still keep getting errors.
但遗憾的是我仍然遇到错误。
library(jsonlite)
multmerge <- function(mypath)
{filenames=list.files(path=mypath, full.names=TRUE)
datalist = lapply(filenames, function(x){fromJSON(file=x)})
Reduce(function(x,y) {merge(x,y)}, datalist)}
mymergeddata <- multmerge("~/Scriptie/LaLiga")
Error in fromJSON(file = x) : argument "txt" is missing, with no default
Is there somebody who knows whats wrong with it? 有谁知道它有什么问题吗?
"@attributes": { "id": 771041269, "event_id": 494, "type_id": "1", "period_id": "2", "min": 57, "sec": 4, "player_id": "17861", "team_id": "186", "outcome": "1", "x": 27.5, "y": 71.8, "assist": "0", "keypass": "0", "timestamp": "2015-09-12T16:09:58.000", "TimeStamp": { "locale": "2015-09-12T16:14:58.000Z", "utc": "2015-09-12T14:14:58.000Z" }, "version": { "lo": 1325465600, "hi": 1605574594 } }, "Q": [ { "@attributes": { "id": 1051565894, "qualifier_id": "141", "value": "45.1" } }, { "@attributes": { "id": 1903370784, "qualifier_id": "140", "value": "12.8" } }, { "@attributes": { "id": 1828708519, "qualifier_id": "212", "value": "23.8" } }, { "@attributes": { "id": 1347694276, "qualifier_id": "5", "value": "" } }, { "@attributes": { "id": 1265264016, "qualifier_id": "56", "value": "Back" } }, { "@attributes": { "id": 1627212783, "qualifier_id": "152", "value": "" } }, { "@attributes": { "id": 1369689442, "qualifier_id": "213", "value": "4.0" } } ] },
Let nl.json
be your input file. 让
nl.json
成为您的输入文件。 I tweaked as follows the json (removing the "@" and including bracket in the beginning and end of the file cf. attached) 我调整如下json(删除“@”并在文件cf.附件的开头和结尾包含括号)
{"attributes": {
"id": 771041269,
"event_id": 494,
"type_id": "1",
"period_id": "2",
"min": 57,
"sec": 4,
"player_id": "17861",
"team_id": "186",
"outcome": "1",
"x": 27.5,
"y": 71.8,
"assist": "0",
"keypass": "0",
"timestamp": "2015-09-12T16:09:58.000",
"TimeStamp": {
"locale": "2015-09-12T16:14:58.000Z",
"utc": "2015-09-12T14:14:58.000Z"
},
"version": {
"lo": 1325465600,
"hi": 1605574594
}
},
"Q": [
{
"@attributes": {
"id": 1051565894,
"qualifier_id": "141",
"value": "45.1"
}
},
{
"@attributes": {
"id": 1903370784,
"qualifier_id": "140",
"value": "12.8"
}
},
{
"@attributes": {
"id": 1828708519,
"qualifier_id": "212",
"value": "23.8"
}
},
{
"@attributes": {
"id": 1347694276,
"qualifier_id": "5",
"value": ""
}
},
{
"@attributes": {
"id": 1265264016,
"qualifier_id": "56",
"value": "Back"
}
},
{
"@attributes": {
"id": 1627212783,
"qualifier_id": "152",
"value": ""
}
},
{
"@attributes": {
"id": 1369689442,
"qualifier_id": "213",
"value": "4.0"
}
}
]
}
}
I also changed the rjson::fromJSON(file="nl.json",method = "R")
yielding in 我也改变了
rjson::fromJSON(file="nl.json",method = "R")
屈服于
$attributes
$attributes$id
[1] 771041269
$attributes$event_id
[1] 494
$attributes$type_id
[1] "1"
$attributes$period_id
[1] "2"
$attributes$min
[1] 57
$attributes$sec
[1] 4
$attributes$player_id
[1] "17861"
$attributes$team_id
[1] "186"
$attributes$outcome
[1] "1"
$attributes$x
[1] 27.5
$attributes$y
[1] 71.8
$attributes$assist
[1] "0"
$attributes$keypass
[1] "0"
$attributes$timestamp
[1] "2015-09-12T16:09:58.000"
$attributes$TimeStamp
$attributes$TimeStamp$locale
[1] "2015-09-12T16:14:58.000Z"
$attributes$TimeStamp$utc
[1] "2015-09-12T14:14:58.000Z"
$attributes$version
$attributes$version$lo
[1] 1325465600
$attributes$version$hi
[1] 1605574594
$Q
$Q[[1]]
$Q[[1]]$`@attributes`
$Q[[1]]$`@attributes`$id
[1] 1051565894
$Q[[1]]$`@attributes`$qualifier_id
[1] "141"
$Q[[1]]$`@attributes`$value
[1] "45.1"
$Q[[2]]
$Q[[2]]$`@attributes`
$Q[[2]]$`@attributes`$id
[1] 1903370784
$Q[[2]]$`@attributes`$qualifier_id
[1] "140"
$Q[[2]]$`@attributes`$value
[1] "12.8"
$Q[[3]]
$Q[[3]]$`@attributes`
$Q[[3]]$`@attributes`$id
[1] 1828708519
$Q[[3]]$`@attributes`$qualifier_id
[1] "212"
$Q[[3]]$`@attributes`$value
[1] "23.8"
$Q[[4]]
$Q[[4]]$`@attributes`
$Q[[4]]$`@attributes`$id
[1] 1347694276
$Q[[4]]$`@attributes`$qualifier_id
[1] "5"
$Q[[4]]$`@attributes`$value
[1] ""
$Q[[5]]
$Q[[5]]$`@attributes`
$Q[[5]]$`@attributes`$id
[1] 1265264016
$Q[[5]]$`@attributes`$qualifier_id
[1] "56"
$Q[[5]]$`@attributes`$value
[1] "Back"
$Q[[6]]
$Q[[6]]$`@attributes`
$Q[[6]]$`@attributes`$id
[1] 1627212783
$Q[[6]]$`@attributes`$qualifier_id
[1] "152"
$Q[[6]]$`@attributes`$value
[1] ""
$Q[[7]]
$Q[[7]]$`@attributes`
$Q[[7]]$`@attributes`$id
[1] 1369689442
$Q[[7]]$`@attributes`$qualifier_id
[1] "213"
$Q[[7]]$`@attributes`$value
[1] "4.0"
Given a function decode_json
that reads your specific JSON data into a data frame, you can efficiently combine the decoding of multiple files into one final data frame as follows: 给定一个函数
decode_json
将您的特定JSON数据读入数据帧,您可以有效地将多个文件的解码组合成一个最终数据帧,如下所示:
import_json_files <- function(files) {
all <- lapply(files, fromJSON)
dec <- lapply(all, decode_json)
bind_rows(dec)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.