Learn R Programming

jsontools (version 0.1.0)

json_unnest_longer: Unnest a JSON array column

Description

Unnest a column of JSON arrays in a data frame producing a longer data frame.

Usage

json_unnest_longer(
  data,
  col,
  values_to = NULL,
  row_numbers_to = NULL,
  indices_to = NULL,
  ptype = NULL,
  wrap_scalars = FALSE,
  bigint_as_char = bigint_default()
)

Arguments

data

A data frame.

col

JSON-column of arrays to extract components from.

values_to

Name of column to store vector values. Defaults to col.

row_numbers_to

Name of column to store the row number before unnesting.

indices_to

Name of column to store the array index of each element; note that this starts with 0.

ptype

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.

wrap_scalars

Should scalar values be wrapped? 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.

bigint_as_char

Convert big integers to character? The option jsontools.bigint_as_char is used as default.

Value

A data frame, or subclass of data frame.

See Also

json_unnest_wider()

Examples

Run this code
# NOT RUN {
df <- tibble::tibble(
  x = c("a", "b"),
  json = c("[1, 2]", "[3, 4, 5]")
)
df

df %>%
  json_unnest_longer(
    "json",
    row_numbers_to = "id",
    indices_to = "index"
  )
# }

Run the code above in your browser using DataLab