Learn R Programming

jsontools (version 0.1.0)

json_unnest_wider: Unnest a JSON object into columns

Description

Unnest a column of JSON objects in a data frame producing a wider data frame.

Usage

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()
)

Arguments

data

A data frame.

col

JSON-column of arrays to extract components from.

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.

names_sort

Should the extracted columns be sorted by name? If FALSE, the default, the columns are sorted by appearance.

names_sep

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.

names_repair

What happens if the output has invalid column names?

wrap_scalars

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.

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 of the same length as data.

See Also

json_unnest_longer()

Examples

Run this code
# 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