I have created three different json objects with R. They look like (I put a small subset of each):
JSON 1:
[
{
"id": "user_F_1",
"group": {
"age": "32"
},
"activity": {
"sport": "football"
},
"scores": {
"2016": "85",
"2017": "87"
}
},
{
"id": "user_F_2",
"group": {
"age": "32"
},
"activity": {
"sport": "hockey"
},
"scores": {
"2016": "62",
"2017": "75"
}
}
]
JSON 2:
[
{
"id": "user_H_1",
"gender": "male",
"region": {
"country": "Finland"
}
},
{
"id": "user_H_2",
"gender": "female",
"region": {
"country": "Greece"
}
}
]
JSON 3:
[
{
"id": "user_Z_1",
"gender": "female",
"age": "35",
"data": {
"continent": "Europe",
"country": "France",
"teamgroup": 3
}
},
{
"id": "user_Z_2",
"gender": "female",
"age": "46",
"data": {
"continent": "Asia",
"country": "China",
"teamgroup": 17
}
}
]
I need to merge them into one single json object. Following what was suggested in this question/27524122 I tried to do:
jsonl <- list(fromJSON(json1), fromJSON(json2), fromJSON(json3))
jsonc <- toJSON(jsonl, pretty=TRUE)
But it concatenates the three of them, not merge. I mean, it returns:
[
{
"id": "user_F_1",
"group": {
"age": "32"
},
"activity": {
"sport": "football"
},
"scores": {
"2016": "85",
"2017": "87"
}
},
{
"id": "user_F_2",
"group": {
"age": "32"
},
"activity": {
"sport": "hockey"
},
"scores": {
"2016": "62",
"2017": "75"
}
}
],
[
{
"id": "user_H_1",
"gender": "male",
"region": {
"country": "Finland"
}
},
{
"id": "user_H_2",
"gender": "female",
"region": {
"country": "Greece"
}
}
],
[
{
"id": "user_Z_1",
"gender": "female",
"age": "35",
"data": {
"continent": "Europe",
"country": "France",
"teamgroup": 3
}
},
{
"id": "user_Z_2",
"gender": "female",
"age": "46",
"data": {
"continent": "Asia",
"country": "China",
"teamgroup": 17
}
}
]
and I need:
[
{
"id": "user_F_1",
"group": {
"age": "32"
},
"activity": {
"sport": "football"
},
"scores": {
"2016": "85",
"2017": "87"
}
},
{
"id": "user_F_2",
"group": {
"age": "32"
},
"activity": {
"sport": "hockey"
},
"scores": {
"2016": "62",
"2017": "75"
}
},
{
"id": "user_H_1",
"gender": "male",
"region": {
"country": "Finland"
}
},
{
"id": "user_H_2",
"gender": "female",
"region": {
"country": "Greece"
}
},
{
"id": "user_Z_1",
"gender": "female",
"age": "35",
"data": {
"continent": "Europe",
"country": "France",
"teamgroup": 3
}
},
{
"id": "user_Z_2",
"gender": "female",
"age": "46",
"data": {
"continent": "Asia",
"country": "China",
"teamgroup": 17
}
}
]
i.e. without the square brackets between the different jsons. All the elements inside one block of square brackets: [{...},{...},{...},{...},{...},{...}]
.
I also tried the paste0
option in this question/47983058, but it's not working either.
How can I achieve this?