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

View source: R/tbl_json.R

tbl_jsonR Documentation

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, ..., .column_order = NULL)

as.tbl_json(.x, ...)

as_tbl_json(.x, ...)

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

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

## S3 method for class 'list'
as.tbl_json(.x, ...)

## S3 method for class '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

...

other arguments

.column_order

Experimental argument to preserve column order for the hidden column

.x

an object to convert into a tbl_json object

json.column

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

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.

Value

a tbl_json object

See Also

read_json for reading json from files

Examples


# 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

jeremystan/tidyjson documentation built on Feb. 4, 2023, 6:54 p.m.