Reputation: 723
I have a basic R question. I am building a list output with the following structure:
headerSpec <- list()
headerSpec$Statistics$columns <- list("item1", "item2", "item3")
headerSpec$Statistics$color = "#C6E0B4"
My expectation when this is exported (via JSON) is a data structure that looks something like this:
headerSpec: {
Statistics: [
columns: [
"item1",
"item2",
"item3"
],
color: "#C6E0B4"
]
}
Unfortunately, my data is instead coming through as:
headerSpec: {
Statistics: [
columns: [
[
"item1"
],
[
"item2"
],
[
"item3"
]
],
color: [
"#C6E0B4"
]
]
}
Or, to put it more simply, the items which I expect to be String data type are instead List of String (with single element, the String value). I am non-native to R so I apologize if terminology is not right.
How do I write these items to produce the desired output?
Upvotes: 0
Views: 37
Reputation: 60060
If you're using jsonlite
to export you can use the auto_unbox
option:
jsonlite::toJSON(headerSpec, auto_unbox = TRUE)
# {"Statistics":{"columns":["item1","item2","item3"],"color":"#C6E0B4"}}
Upvotes: 1
Reputation: 520888
Try unlisting your list into a character vector:
headerSpec <- list()
headerSpec$Statistics$columns <- list("item1", "item2", "item3")
headerSpec$Statistics$color = "#C6E0B4"
headerSpec$Statistics$columns <- unlist(headerSpec$Statistics$columns)
Upvotes: 1