Not a json expert, but I need what I think is referred to as "nested objects" and I am getting instead what I think is referred to as "nested arrays". In other words, some extra brackets. I'm trying to convert a dataframe into json data using jsonlite in R. Reproducible code and results below. Can anyone point me to how to get the data in the proper format (rows as nested objects)?
library(jsonlite)
testdat <- data.frame(locationColumn = c("US", "US"),
nameColumn = c("General Motors", "Walmart"),
zipColumn = c(19890, 72712) )
jsl <- jsonlite::toJSON(
list(
rows = split(testdat, 1:nrow(testdat))
),
auto_unbox = TRUE,
pretty = TRUE,
dataframe = "rows",
simplifyDataFrame = TRUE
)
jsl
Output:
{
"rows": {
"1": [
{
"locationColumn": "US",
"nameColumn": "General Motors",
"zipColumn": 19890
}
],
"2": [
{
"locationColumn": "US",
"nameColumn": "Walmart",
"zipColumn": 72712
}
]
}
}
What I need:
{
"rows": {
"1":{
"locationColumn": "US",
"nameColumn": "General Motors",
"zipColumn": 19890
},
"2":{
"locationColumn": "US",
"nameColumn": "Walmart",
"zipColumn": 72712
}
}
}