tidyjson (version 0.2.4)

tbl_json: Combines structured JSON (as a data.frame) with remaining JSON

Description

Constructs a tbl_json object, for further downstream manipulation by other tidyjson functions. Methods exist to convert JSON stored in character strings without any other associated data, as a separate character string and associated data frame, or as a single data frame with a specified character string JSON column.

Usage

tbl_json(df, json.list, drop.null.json = FALSE)

as.tbl_json(.x, ...)

# S3 method for tbl_json as.tbl_json(.x, ...)

# S3 method for character as.tbl_json(.x, ...)

# S3 method for data.frame as.tbl_json(.x, json.column, ...)

is.tbl_json(.x)

Arguments

df

data.frame

json.list

list of json lists parsed with fromJSON

drop.null.json

drop NULL json entries from df and json.list

.x

an object to convert into a tbl_json object

...

other arguments

json.column

the name of the json column of data in .x, if .x is a data frame

Value

a tbl_json object

Details

Most tidyjson functions accept a tbl_json object as the first argument, and return a tbl_json object unless otherwise specified. tidyjson functions will attempt to convert an object that isn't a tbl_json object first, and so explicit construction of tidyjson objects is rarely needed.

tbl_json objects consist of a data frame along with it's associated JSON, where each row of the data frame corresponds to a single JSON document. The JSON is stored in a "JSON" attribute.

Note that json.list must have the same length as nrow(df), and if json.list has any NULL elements, the corresponding rows will be removed from df. Also note that "..JSON" is a reserved column name used internally for filtering tbl_json objects, and so is not allowed in the names of df.

See Also

read_json for reading json from files

Examples

Run this code
# NOT RUN {
# Construct a tbl_json object using a charater string of JSON
json <- '{"animal": "cat", "count": 2}'
json %>% as.tbl_json

# access the "JSON" argument
json %>% as.tbl_json %>% attr("JSON")

# Construct a tbl_json object using multiple documents
json <- c('{"animal": "cat", "count": 2}', '{"animal": "parrot", "count": 1}')
json %>% as.tbl_json

# Construct a tbl_json object from a data.frame with a JSON colum
library(tibble)
farms <- tribble(
  ~farm, ~animals,
  1L,    '[{"animal": "pig", "count": 50}, {"animal": "cow", "count": 10}]',
  2L,    '[{"animal": "chicken", "count": 20}]'
)
farms %>% as.tbl_json(json.column = "animals")
# tidy the farms
farms %>% as.tbl_json(json.column = "animals") %>%
  gather_array %>% spread_all
# }

Run the code above in your browser using DataLab