Learn R Programming

jsontools (version 0.1.0)

format_json_rowwise: Convert each tbl row to JSON

Description

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

Usage

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?

Value

A json2 vector of JSON objects.

Details

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

See Also

write_json(), format_json()

Examples

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

Run the code above in your browser using DataLab