to_json: To JSON

Description Usage Arguments Examples

View source: R/to_json.R

Description

Converts R objects to JSON

Usage

1
2
3
4
5
6
7
8
to_json(
  x,
  unbox = FALSE,
  digits = NULL,
  numeric_dates = TRUE,
  factors_as_string = TRUE,
  by = "row"
)

Arguments

x

object to convert to JSON

unbox

logical indicating if single-value arrays should be 'unboxed', that is, not contained inside an array.

digits

integer specifying the number of decimal places to round numerics. Default is NULL - no rounding

numeric_dates

logical indicating if dates should be treated as numerics. Defaults to TRUE for speed. If FALSE, the dates will be coerced to character in UTC time zone

factors_as_string

logical indicating if factors should be treated as strings. Defaults to TRUE.

by

either "row" or "column" indicating if data.frames and matrices should be processed row-wise or column-wise. Defaults to "row"

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
to_json(1:3)
to_json(letters[1:3])

## factors treated as strings
to_json(data.frame(x = 1:3, y = letters[1:3], stringsAsFactors = TRUE ))
to_json(data.frame(x = 1:3, y = letters[1:3], stringsAsFactors = FALSE ))

to_json(list(x = 1:3, y = list(z = letters[1:3])))
to_json(seq(as.Date("2018-01-01"), as.Date("2018-01-05"), length.out = 5))
to_json(seq(as.Date("2018-01-01"), as.Date("2018-01-05"), length.out = 5), numeric_dates = FALSE)

psx <- seq(
  as.POSIXct("2018-01-01", tz = "Australia/Melbourne"), 
  as.POSIXct("2018-02-01", tz = "Australia/Melbourne"), 
  length.out = 5
  )
to_json(psx)
to_json(psx, numeric_dates = FALSE)

## unbox single-value arrays
to_json(list(x = 1), unbox = TRUE)
to_json(list(x = 1, y = c("a"), z = list(x = 2, y = c("b"))), unbox = TRUE)

## rounding numbers using the digits argument
to_json(1.23456789, digits = 2)
df <- data.frame(x = 1L:3L, y = rnorm(3), z = letters[1:3], stringsAsFactors = TRUE )
to_json(df, digits = 0 )

## keeping factors
to_json(df, digits = 2, factors_as_string = FALSE )

jsonify documentation built on July 2, 2020, 2:55 a.m.