Search code examples
jsonrreactjsnvd3.jsjsonlite

Add key and value to JSON in R


I used jsonlite library: json_pca_data <- toJSON(pca_table, pretty = TRUE)) to produce JSON:

{
    "neuron_type" : [
        "Ciliated_sensory_neurons",
        "Touch_receptor_neurons",
        ...
        ...
    ],
    "PC1" : [
        4.1158,
        -1.1647,
        ...
        ...
    ],
    "PC2" : [
        -1.4615,
        1.9541,
        ...
        ...
    ],
    "octr-1" : [
        2.5517,
        2.8857,
        ...
        ...

from the pca_table that looks like that:

neuron_type                      PC1              PC2          octr-1    
Ciliated_sensory_neurons      4.1157653       -1.4614620      2.551738 
Touch_receptor_neurons       -1.1647174        1.9540974      2.885656 
...
...

But I want to add to the final JSON also PC1: 0.36 and PC2: 0.21. I can just modify the initial pca_table:

pca_table$PC1_percent = percent[1]
pca_table$PC2_percent = percent[2]

Adding two columns and then transforming it into JSON, however, I do not like to have

"PC1_percent" : [
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676
],
"PC2_percent" : [
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331
]

I want to have just key and value instead:

"PC1_percent" : 0.3676, "PC2_percent" : 0.2331

Is there a way to do that in Rstudio?


Solution

  • I agree with your logic to change the underlying data vice editing JSON. Your first problem is that you are replacing the column values with a single one, instead of appending it.

    pca_table$PC1_percent = c(pca_table$PC1_percent, percent[1])
    

    would append the first value of percent onto the end of the PC1_percent column. Unfortunately, this will likely error (something like replacement has 3 rows, data has 2) or will produce other undesirable effects.

    It helps to know if you intend to append a value to all columns or if you just want to extend those two rows. For both methods, my fake data:

    tbl <- data.frame(pc1=1:2, pc2=11:12, othr=51)
    tbl
    #   pc1 pc2 othr
    # 1   1  11   51
    # 2   2  12   51
    

    Extend all columns

    Make a new row, representative of the original data.frame (you need to include something for all columns):

    newrow <- data.frame(pc1=3, pc2=13, othr=NA)
    tbl2 <- rbind(tbl, newrow)
    tbl2
    #   pc1 pc2 othr
    # 1   1  11   51
    # 2   2  12   51
    # 3   3  13   NA
    jsonlite::toJSON(tbl2, dataframe="columns")
    # {"pc1":[1,2,3],"pc2":[11,12,13],"othr":[51,51,"NA"]} 
    

    (I'm inferring "columns" from your problem, even if you didn't specify that in your call. I'm omitting pretty=TRUE for brevity.)

    Extend only some columns

    By going this route, you change from a data.frame to a list, since a data.frame is roughly a list of vectors where all vectors are the same length.

    lst <- as.list(tbl)
    lst$pc1 <- c(lst$pc1, 98)
    lst$pc2 <- c(lst$pc2, 99)
    jsonlite::toJSON(lst)
    # {"pc1":[1,2,98],"pc2":[11,12,99],"othr":[51,51]}