spread_all: Spreads all scalar values of a JSON object into new columns

Description Usage Arguments Details Value See Also Examples

View source: R/spread_all.R

Description

Like the spread function in tidyr but for JSON, this function spreads out any JSON objects that are scalars into new columns. If objects are nested, then the recursive flag will expand scalar values of nested objects out with a compound column name based on the sequences of nested object names concatenated with the sep character.

Usage

1
spread_all(.x, recursive = TRUE, sep = ".")

Arguments

.x

a json string or tbl_json object

recursive

whether or not to recursively spread nested objects

sep

character used to separate nested object names when resursive is TRUE

Details

Note that arrays are ignored by this function, use gather_array to gather the array first, and then use spread_all if the array contains objects or use one of the append_values functions to capture the array values if they are scalars.

Note that scalar JSON values (e.g., a JSON string like '1') are also ignored, as they have no names to create column names with.

The order of columns is determined by the order they are encountered in the JSON document, with nested objects placed at the end.

If an objects have name-value pairs with names that are duplicates, then ".n" is appended for n incrementing from 2 on to ensure that columns are unique. This also happens if .x already has a column with the name of a name-value pair.

This function does not change the value of the JSON attribute of the tbl_json object in any way.

Value

a tbl_json object

See Also

spread_values to specific which specific values to spread along with their types, spread for spreading data frames

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# A simple example
json <- c('{"a": "x", "b": 1, "c": true}',
          '{"a": "y", "c": false}',
          '{"a": null, "d": "z"}')
json %>% spread_all

# A more complex example
worldbank %>% spread_all

# Resolving duplicate column names
json <- '{"a": "x", "a": "y"}'
json %>% spread_all

jeremystan/tidyjson documentation built on May 19, 2019, 5:09 a.m.