tidyjson (version 0.2.4)

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

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

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

Value

a tbl_json object

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.

See Also

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

Examples

Run this code
# NOT RUN {
# 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
# }

Run the code above in your browser using DataCamp Workspace