Reputation: 21
I am currently trying to merge multiple (380) JSON files into one dataframe.
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"
}
}
]
},
Upvotes: 2
Views: 1551
Reputation: 664
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:
import_json_files <- function(files) {
all <- lapply(files, fromJSON)
dec <- lapply(all, decode_json)
bind_rows(dec)
}
Upvotes: 1
Reputation: 1795
Let nl.json
be your input file. I tweaked as follows the json (removing the "@" and including bracket in the beginning and end of the file cf. attached)
{"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
$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"
Upvotes: 0