format_json_rowwise: Convert each tbl row to JSON

Description Usage Arguments Details Value See Also Examples

View source: R/format_json_rowwise.R

Description

Convert each row of a data frame to a JSON object (this basically produces ndjson).

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
format_json_rowwise(
  df,
  null = c("list", "null"),
  na = c("null", "string"),
  auto_unbox = TRUE,
  matrix = c("rowmajor", "columnmajor"),
  Date = c("ISO8601", "epoch"),
  POSIXt = c("string", "ISO8601", "epoch", "mongo"),
  factor = c("string", "integer"),
  complex = c("string", "list"),
  raw = c("base64", "hex", "mongo"),
  digits = 4,
  json_verbatim = TRUE,
  force = FALSE,
  rownames = FALSE,
  ...
)

Arguments

df

A dataframe.

null, na, POSIXt, factor, complex, matrix, Date

passed on to jsonlite::stream_out.

auto_unbox, raw, digits, force, ...

passed on to jsonlite::stream_out.

json_verbatim

Leave JSON as it is and do not encode it again?

rownames

For data.frames add a field _row with the row name?

Details

Under the hood jsonlite::stream_out is used for the conversion.

Value

A json2 vector of JSON objects.

See Also

write_json(), format_json()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
format_json_rowwise(mtcars[1:3, ])

# use a dataframe column for nested objects
df <- data.frame(x = 1:2)
df$y <- data.frame(z = c("a", "b"))
format_json_rowwise(df)

if (require("dplyr", quietly = TRUE, warn.conflicts = FALSE)) {
  tibble <- tibble::tibble
  # often useful in mutate/transmute
  mtcars %>%
    transmute(json = format_json_rowwise(tibble(mpg, cyl, extra = tibble(disp, hp))))
}

jsontools documentation built on March 22, 2021, 5:06 p.m.