append_values: Appends all JSON values with a specified type as a new column

append_valuesR Documentation

Appends all JSON values with a specified type as a new column

Description

The append_values functions let you take any scalar JSON values of a given type ("string", "number", "logical") and add them as a new column named column.name. This is particularly useful after using gather_object to gather an object.

Usage

append_values_string(.x, column.name = type, force = TRUE, recursive = FALSE)

append_values_number(.x, column.name = type, force = TRUE, recursive = FALSE)

append_values_logical(.x, column.name = type, force = TRUE, recursive = FALSE)

Arguments

.x

a json string or tbl_json object

column.name

the name of the column to append values as

force

should values be coerced to the appropriate type when possible, otherwise, types are checked first (requires more memory)

recursive

logical indicating whether to recurisvely extract a single value from a nested object. Only used when force = TRUE. If force = FALSE, and recursive = TRUE, throws an error.

Details

Any values that can not be converted to the specified will be NA in the resulting column. This includes other scalar types (e.g., numbers or logicals if you are using append_values_string) and *also* any rows where the JSON is NULL or an object or array.

Note that the append_values functions do not alter the JSON attribute of the tbl_json object in any way.

Value

a tbl_json object

See Also

gather_object to gather an object first, spread_all to spread values into new columns, json_get_column

Examples


# Stack names
'{"first": "bob", "last": "jones"}' %>%
  gather_object %>%
  append_values_string

# This is most useful when data is stored in name-value pairs
# For example, tags in recipes:
recipes <- c('{"name": "pie", "tags": {"apple": 10, "pie": 2, "flour": 5}}',
             '{"name": "cookie", "tags": {"chocolate": 2, "cookie": 1}}')
recipes %>%
  spread_values(name = jstring(name)) %>%
  enter_object(tags) %>%
  gather_object("tag") %>%
  append_values_number("count")

tidyjson documentation built on Jan. 7, 2023, 1:14 a.m.