spread_values: Spreads specific scalar values of a JSON object into new...

View source: R/spread_values.R

spread_valuesR Documentation

Spreads specific scalar values of a JSON object into new columns

Description

The spread_values function lets you extract extract specific values from (potentiall nested) JSON objects. spread_values takes jstring, jnumber or jlogical named function calls as arguments in order to specify the type of the data that should be captured at each desired name-value pair location. These values can be of varying types at varying depths.

Usage

spread_values(.x, ...)

Arguments

.x

a json string or tbl_json object

...

column = value pairs where column will be the column name created and value must be a call to jstring, jnumber or jlogical specifying the path to get the value (and the type implicit in the function name)

Details

Note that jstring, jnumber and jlogical will fail if they encounter the incorrect type in any document.

The advantage of spread_values over spread_all is that you are guaranteed to get a consistent data frame structure (columns and types) out of any spread_values call. spread_all requires less typing, but because it infers the columns and their types from the JSON, it is less suitable when programming.

Value

a tbl_json object

See Also

spread_all for spreading all values, spread for spreading data frames, jstring, jnumber, jlogical for accessing specific names

Examples


# A simple example
json <- '{"name": {"first": "Bob", "last": "Jones"}, "age": 32}'

# Using spread_values
json %>%
  spread_values(
    first.name = jstring(name, first),
    last.name  = jstring(name, last),
    age        = jnumber(age)
  )

# Another document, this time with a middle name (and no age)
json2 <- '{"name": {"first": "Ann", "middle": "A", "last": "Smith"}}'

# spread_values still gives the same column structure
c(json, json2) %>%
  spread_values(
    first.name = jstring(name, first),
    last.name  = jstring(name, last),
    age        = jnumber(age)
  )

# whereas spread_all adds a new column
json %>% spread_all
c(json, json2) %>% spread_all

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