wraps a data frame in a list and or unboxes list items that are 1 row dataframes. This will result in an array of objects being created.
See also
Other JSON Prep:
clean_field_names(),
get_entity(),
prep_affiliation(),
prep_array(),
prep_atomic(),
prep_creators(),
prep_data(),
prep_descriptions(),
prep_for_json(),
prep_from_metadata_template(),
prep_fundingReferences(),
prep_identifier(),
prep_language(),
prep_methodology(),
prep_methods(),
prep_nameIdentifiers(),
prep_object(),
prep_publicationYear(),
prep_relatedIdentifiers(),
prep_rights(),
prep_subjects(),
prep_titles()
Examples
# note that you cannot unbox data frames with more than 1 row
x <- list(
tibble::tibble(age = 1, group = letters[1]),
tibble::tibble(age = 2, group = letters[2])
)
# running jsonlite::toJSON on an unmodified object results in
# extra square brackets - an array of arrays of objects
jsonlite::toJSON(x, pretty = TRUE)
#> [
#> [
#> {
#> "age": 1,
#> "group": "a"
#> }
#> ],
#> [
#> {
#> "age": 2,
#> "group": "b"
#> }
#> ]
#> ]
# with the prepped data we get an array of objects
x_prepped <- prep_array_objects(x)
x_prepped |>
jsonlite::toJSON(pretty = TRUE)
#> [
#> {
#> "age": 1,
#> "group": "a"
#> },
#> {
#> "age": 2,
#> "group": "b"
#> }
#> ]