Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

constructing json in R with jsonlite - too many brackets

$
0
0

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
        }
  }
}

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>