Using jsonlite in R to output json in a specific format

20 views Asked by At

I'm trying to output some data as json in a specific format to be used in other processes. I've got it mostly correct, but I'm having trouble with one specific key/value pair.

Here is a simplified code snippet:

library(jsonlite)

data_type_name <- "radians"
x_list <- c(100, 200, 350)
y_list <- c(20, 10, 70)

dataList <- list(name = data_type_name, x_list = list(x_list), y_list = list(y_list))

dataJson <- toJSON(dataList, pretty = 4)
print(dataJson)

This gives me the following output:

{
    "name": [
        "radians"
    ],
    "x_list": [
        [
            100,
            200,
            350
        ]
    ],
    "y_list": [
        [
            20,
            10,
            70
        ]
    ]
}

My issue is that I need "name" and "radians" to be a key/value pair rather than a key and a list. In other words '"name": "radians"'. I've looked around for a solution for this for hours but I'm coming up short. I appreciate whatever assistance you can provide.

0

There are 0 answers