Dennis Ich Dennis Ich - 3 months ago 10
JSON Question

R Summary to parsable format preferable JSON

Is there a way to get a clean output from

summary(df)
as JSON or a dataframe where not everything has excess whitespace and the actual things like "Min: 3" are in one column? I would like to have something like this:

[
{
"column1": {
"Min": 1,
"Max": 3,
"3rd Qu": 5
...
}
},

{
"column2": {
"Min": 2,
"Max": 4,
"3rd Qu": 5,
...
}
}
]

Answer

There are plenty of options for converting to JSON, but you first need to get your data into a format that will convert the way you want (or at all). Thus, it's easiest to assemble the summary as a list so you can determine the hierarchy of the JSON. Using piping and purrr::map (like lapply) for readability:

library(purrr)

iris %>% map(summary) %>%    # make a list of summaries in case both numeric/factor
    map(as.list) %>%     # coerce each summary to a list
    jsonlite::toJSON(auto_unbox = TRUE, pretty = TRUE)   # convert to JSON, make it legible

which returns

{
  "Sepal.Length": {
    "Min.": 4.3,
    "1st Qu.": 5.1,
    "Median": 5.8,
    "Mean": 5.843,
    "3rd Qu.": 6.4,
    "Max.": 7.9
  },
  "Sepal.Width": {
    "Min.": 2,
    "1st Qu.": 2.8,
    "Median": 3,
    "Mean": 3.057,
    "3rd Qu.": 3.3,
    "Max.": 4.4
  },
  "Petal.Length": {
    "Min.": 1,
    "1st Qu.": 1.6,
    "Median": 4.35,
    "Mean": 3.758,
    "3rd Qu.": 5.1,
    "Max.": 6.9
  },
  "Petal.Width": {
    "Min.": 0.1,
    "1st Qu.": 0.3,
    "Median": 1.3,
    "Mean": 1.199,
    "3rd Qu.": 1.8,
    "Max.": 2.5
  },
  "Species": {
    "setosa": 50,
    "versicolor": 50,
    "virginica": 50
  }
} 

If you'd don't pipe, you can do it without purrr:

jsonlite::toJSON(lapply(iris, function(x){as.list(summary(x))}), pretty = TRUE, auto_unbox = TRUE)

which returns the same thing.