format_json: Convert R objects to JSON

Description Usage Arguments Value See Also Examples

View source: R/format_json.R

Description

format_json is only a wrapper around the great jsonlite::toJSON(). The differences are

format_json_list() converts each element of a list to JSON.

To make sure that a length one vector is not turned into an array use json_u() or jsonlite::unbox().

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
format_json(
  x,
  null = c("list", "null"),
  na = c("null", "string"),
  auto_unbox = FALSE,
  dataframe = c("rows", "columns", "values"),
  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", "int", "js"),
  digits = 4,
  json_verbatim = TRUE,
  force = FALSE,
  pretty = FALSE,
  rownames = FALSE,
  always_decimal = FALSE,
  ...
)

format_json_list(
  x,
  null = c("list", "null"),
  na = c("null", "string"),
  auto_unbox = FALSE,
  dataframe = c("rows", "columns", "values"),
  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", "int", "js"),
  digits = 4,
  json_verbatim = TRUE,
  force = FALSE,
  pretty = FALSE,
  rownames = FALSE,
  always_decimal = FALSE,
  ...
)

Arguments

x

the object to be encoded

null, na, auto_unbox, dataframe, matrix, Date

passed on to jsonlite::toJSON.

POSIXt, factor, complex, raw, digits, force, pretty, ...

passed on to jsonlite::toJSON.

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?

always_decimal

Use real number notation in whole number doubles?

Value

A json2 vector.

See Also

write_json(), format_json_rowwise()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# null
x_null <- list(a = NULL, b = 1)
format_json(x_null)
format_json(x_null, null = "null")

# na
x_na <- list(a = NA, b = 1)
format_json(x_na)
format_json(x_na, na = "string")

# auto_unbox
x_autounbox <- list(1, 1:3)
format_json(x_autounbox)
format_json(x_autounbox, auto_unbox = TRUE)

# dataframe conversion
x_df <- iris[1:2, ]
format_json(x_df, pretty = TRUE)
format_json(x_df, dataframe = "columns", pretty = TRUE)
format_json(x_df, dataframe = "values", pretty = TRUE)

# json_verbatim
x_json <- json2('["a","b"]')
format_json(x_json)
format_json(x_json, json_verbatim = FALSE)

# Decimal vs significant digits
x <- 10 + pi
format_json(x)
format_json(x, digits = NA)
format_json(x, digits = 2)
format_json(x, digits = I(2))

# Force decimal representation
format_json(12)
format_json(12, always_decimal = TRUE)

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