flattenj_one {mojson} | R Documentation |
Single JSON Object Flatten
Description
Transform a JSON object into a flattened data frame in a serialization way.
Usage
flattenj_one(dat, sep = "@", compact = TRUE)
Arguments
dat |
|
sep |
|
compact |
logical. Whether to generate the compact or completely expanded data frame. Defaults to |
Details
The function flattens a single JSON object into a data frame with two different schemas according to the compact
value.
For
compact = TRUE
, the data frame contains two columns. One ispaths
which stores the absolute path of each record. And the other isvalues
which stores the corresponding values of each path.For
compact = FALSE
, the data frame has more columns based on the global nesting situation.
It actually applies the serialization way for flattening, which means the early values correspondingly appear in the heading rows of the data frame.
And if the value is a list object in the original data or a non-named list/vector in the R environment,
the path will be correspondingly appended with an integer to specify each list element.
For example, in the raw JSON file, "{'a':[1, 2, 3]}" will be data.frame(paths = c('a1', 'a2', 'a3'), values = c(1, 2, 3))
.
Great credits to the answer of Tommy.
Value
data frame
. The flattened result.
See Also
Examples
library(mojson)
j <- list(a = list(x = 1, y = 2),
b = c(3, 4, list(z = 5, s = 6, t = list(m = 7, n = 8))))
flattenj_one(j)
flattenj_one(j, compact = FALSE)