Last chance! 50% off unlimited learning
Sale ends in
Unnest a column of JSON objects in a data frame producing a wider data frame.
json_unnest_wider(
data,
col,
ptype = list(),
names_sort = FALSE,
names_sep = NULL,
names_repair = "check_unique",
wrap_scalars = FALSE,
bigint_as_char = bigint_default()
)
A data frame.
JSON-column of arrays to extract components from.
Output type. If NULL
, the default, the output type is
determined by computing the common type across all elements. Use
new_json_array()
resp. new_json_object()
if you know every element is
an array resp. object. Mind that the return type will only be json2
.
Should the extracted columns be sorted by name? If FALSE
,
the default, the columns are sorted by appearance.
If NULL
, the default, the keys of the objects in col
are used as the column names. If a character it is used to join col
and
the object keys.
What happens if the output has invalid column names?
A named list of TRUE
or FALSE
specifying for each
field whether to wrap scalar values in a JSON array. Unspecified fields
are not wrapped. This can also be a single value of TRUE
or FALSE
that is then used for every field.
Note that scalars are only wrapped if either
ptype
is new_json_array()
or json2
vector.
ptype
is NULL
and the elements are a mix of scalar values and arrays.
Convert big integers to character? The option
jsontools.bigint_as_char
is used as default.
A data frame, or subclass of data frame of the same length as data
.
# NOT RUN {
# turn all components of item into columns with json_unnest_wider()
tibble::tibble(
id = 1:2,
x = c(
'{"name": "Peter", "age": 19}',
'{"age": 37}'
)
) %>%
json_unnest_wider(x)
# sort names and specify proto types
tibble::tibble(
id = 1:2,
x = c(
'{"name": "Peter", "age": 19, "purchase_ids": [1, 2]}',
'{"age": 37, "purchase_ids": []}'
)
) %>%
json_unnest_wider(
x,
ptype = list(
age = integer(),
name = character(),
purchase_id = new_json_array()
),
names_sort = TRUE
)
# }
Run the code above in your browser using DataLab