json: Convert to/from JSON

Description Usage Arguments Details See Also

Description

These functions wrap jsonlite::fromJSON and jsonlite::toJSON and make it easier to work with JSON inside of a data set.

Usage

1
2
3
from_JSON(x, keep_na = TRUE, ...)

to_JSON(x, keep_na = TRUE, rm_class = TRUE, ...)

Arguments

x

An object to be converted to/from JSON.

keep_na

A single logical value indicating whether to return NA values when received (TRUE) or not (FALSE).

...

Additional arguments to be passed to jsonlite functions.

rm_class

A single logical value indicating whether to remove the "json" class from JSON objects (TRUE) or not (FALSE).

Details

A common use case is where all selected answers to a multi-select checkbox survey question are stored in a single column of a wide-formatted data set. JSON encoding is a simply way to ensure answer text doesn't interfere with the process of parsing the different answers (e.g. if there was a semi-colon in the answer and your separator was a semi-colon).

jsonlite does not support the preservation of NA values that would be needed for this use case, such as when someone skipped the multi-select question. jsonlite throws an error reading an NA value and writes NA to a JSON object containing an NA value, instead of simply returning NA. To make the functions more friendly for data sets, NA values are returned as NA during both reading and writing.

When storing JSON in data frames, JSON objects will be stored as a character vector and be stripped of their "json" class that jsonlite attaches. Losing the "json" class does not have any detrimental effects, but unneccessary warnings may be thrown if data frame functions (e.g. dplyr::mutate) have to remove the class for you. to_JSON strips the "json" class by default to prevent warning spam.

See Also

fromJSON, toJSON


TheFridayInstitute/fimoodler documentation built on May 28, 2019, 9:37 a.m.