Learn R Programming

jsontools (version 0.1.0)

json_flatten: Flatten a JSON array

Description

Flatten a JSON array

Usage

json_flatten(
  x,
  ptype = NULL,
  allow_scalars = FALSE,
  wrap_scalars = FALSE,
  bigint_as_char = bigint_default()
)

Arguments

x

A JSON vector.

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.

allow_scalars

Do not error for scalar elements?

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 flattened version of x with class given by ptype and length equal to the sum of the json_array_length() of the components of x.

See Also

json_unnest_longer(), json_unnest_wider()

Examples

Run this code
# NOT RUN {
json_flatten(c("[1, 2]", "[3]"))

# names are kep
json_flatten(c(x = "[1, 2]", y = "[3]"))

# scalar elements produce an error ...
try(json_flatten(c(x = "[1, 2]", y = "3")))
# ... but can be explicitly allowed with `allow_scalars`
json_flatten(c(x = "[1, 2]", y = "3"), allow_scalars = TRUE)
# }

Run the code above in your browser using DataLab