Converts a named vector, list, or data frame to a list, and optionally unboxes it, so that its recorded as an object.
Details
Note that unboxing will only work on items where you have 1:1 key value pair. So if you have a dataframe with multiple rows or a list with multiple values at a given position, it won't work.
See also
Other JSON Prep:
clean_field_names()
,
get_entity()
,
prep_affiliation()
,
prep_array_objects()
,
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_publicationYear()
,
prep_relatedIdentifiers()
,
prep_rights()
,
prep_subjects()
,
prep_titles()
Examples
cars_small <- datasets::cars[1:10, ]
# creates an array of objects where each
# row is an object
cars_small |>
jsonlite::toJSON(pretty = TRUE)
#> [
#> {
#> "speed": 4,
#> "dist": 2
#> },
#> {
#> "speed": 4,
#> "dist": 10
#> },
#> {
#> "speed": 7,
#> "dist": 4
#> },
#> {
#> "speed": 7,
#> "dist": 22
#> },
#> {
#> "speed": 8,
#> "dist": 16
#> },
#> {
#> "speed": 9,
#> "dist": 10
#> },
#> {
#> "speed": 10,
#> "dist": 18
#> },
#> {
#> "speed": 10,
#> "dist": 26
#> },
#> {
#> "speed": 10,
#> "dist": 34
#> },
#> {
#> "speed": 11,
#> "dist": 17
#> }
#> ]
# creates an object with 2 arrays
prep_object(cars_small) |>
jsonlite::toJSON(pretty = TRUE)
#> {
#> "speed": [4, 4, 7, 7, 8, 9, 10, 10, 10, 11],
#> "dist": [2, 10, 4, 22, 16, 10, 18, 26, 34, 17]
#> }
# this makes no difference
x <- list("hello" = 1:10, "world" = "Earth")
prep_object(x) |>
jsonlite::toJSON(pretty = TRUE)
#> {
#> "hello": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
#> "world": ["Earth"]
#> }