2

I have two separate objects in R which have a JSON format and I'm trying to convert into a single R objects with two JSON objects in it's structure.

When I create a list with the two objects then concatenate with toJSON() it creates the two JSON objects but I lost the JSON sub structure and objects are flat.

For example :

json <- list(obj1, obj2)
names(json) <- c("object1", "object2")  
json <- toJSON(json)

The result looks like this :

{
    "object1": ["{\"cum\":[[[1421020800000, -0.0618],[1422835200000, 0.3907] ... "],
    "object2": ["{\"cum\":[[[1421020800000, -0.015],[1422835200000, 0.3447] ... "]
}

This is my objets structure.

obj1:

{
    "cum": [
        [
            [1421020800000, -0.0618],
            [1422835200000, 0.3907]
        ]
    ],
    "alloc": {
        "Current": [0.36, 0.725, 0.074, 0.473, 0.029, 10, 0.46, 0.414, 0.965],
    },
    "time": [14],
    "position": [15.14]
}

obj2:

{
    "cum": [
        [
            [1421020800000, -0.015],
            [1422835200000, 0.3447]
        ]
    ],
    "alloc": {
        "Current": [0.6, 0.5, 0.04, 0.3, 0.09, 1, 0.6, 0.44, 0.5],
    },
    "time": [19],
    "position": [1.09]
}

And this is what I'm trying to achieve :

{
    "object1": {
        "cum": [
            [
                [1421020800000, -0.0618],
                [1422835200000, 0.3907]
            ]
        ],
        "alloc": {
            "Current": [0.36, 0.725, 0.074, 0.473, 0.029, 10, 0.46, 0.414, 0.965]
        },
        "time": [14],
        "position": [15.14]
    },

    "object2": {
        "cum": [
            [
                [1421020800000, -0.015],
                [1422835200000, 0.3447]
            ]
        ],
        "alloc": {
            "Current": [0.6, 0.5, 0.04, 0.3, 0.09, 1, 0.6, 0.44, 0.5]
        },
        "time": [19],
        "position": [1.09]
    }
}

Thank you,

Florent
  • 1,791
  • 22
  • 40

1 Answers1

3

I just found the answer to my own question, by simply using paste0 instead of list() and toJSON.

json <- paste0('{"object1":', json1, ',"object2":', json2, '}' )
Florent
  • 1,791
  • 22
  • 40