Converts R objects to ndjson
to_ndjson(
x,
unbox = FALSE,
digits = NULL,
numeric_dates = TRUE,
factors_as_string = TRUE,
by = "row"
)
object to convert to JSON
logical indicating if single-value arrays should be 'unboxed', that is, not contained inside an array.
integer specifying the number of decimal places to round numerics.
Default is NULL
- no rounding
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
logical indicating if factors should be treated as strings. Defaults to TRUE.
either "row" or "column" indicating if data.frames and matrices should be processed row-wise or column-wise. Defaults to "row"
Lists are converted to ndjson non-recursively. That is, each of the objects in the list at the top level are converted to a new-line JSON object. Any nested sub-elements are then contained within that JSON object. See examples
to_ndjson( 1:5 )
#> [1,2,3,4,5]
to_ndjson( letters )
#> ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
mat <- matrix(1:6, ncol = 2)
to_ndjson( x = mat )
#> [1,4]
#> [2,5]
#> [3,6]
to_ndjson( x = mat, by = "col" )
#> [1,2,3]
#> [4,5,6]
df <- data.frame(
x = 1:5
, y = letters[1:5]
, z = as.Date(seq(18262, 18262 + 4, by = 1 ), origin = "1970-01-01" )
)
to_ndjson( x = df )
#> {"x":1,"y":"a","z":18262.0}
#> {"x":2,"y":"b","z":18263.0}
#> {"x":3,"y":"c","z":18264.0}
#> {"x":4,"y":"d","z":18265.0}
#> {"x":5,"y":"e","z":18266.0}
to_ndjson( x = df, numeric_dates = FALSE )
#> {"x":1,"y":"a","z":"2020-01-01"}
#> {"x":2,"y":"b","z":"2020-01-02"}
#> {"x":3,"y":"c","z":"2020-01-03"}
#> {"x":4,"y":"d","z":"2020-01-04"}
#> {"x":5,"y":"e","z":"2020-01-05"}
to_ndjson( x = df, factors_as_string = FALSE )
#> {"x":1,"y":"a","z":18262.0}
#> {"x":2,"y":"b","z":18263.0}
#> {"x":3,"y":"c","z":18264.0}
#> {"x":4,"y":"d","z":18265.0}
#> {"x":5,"y":"e","z":18266.0}
to_ndjson( x = df, by = "column" )
#> {"x":[1,2,3,4,5]}
#> {"y":["a","b","c","d","e"]}
#> {"z":[18262.0,18263.0,18264.0,18265.0,18266.0]}
to_ndjson( x = df, by = "column", numeric_dates = FALSE )
#> {"x":[1,2,3,4,5]}
#> {"y":["a","b","c","d","e"]}
#> {"z":["2020-01-01","2020-01-02","2020-01-03","2020-01-04","2020-01-05"]}
## Lists are non-recurisve; only elements `x` and `y` are converted to ndjson
lst <- list(
x = 1:5
, y = list(
a = letters[1:5]
, b = data.frame(i = 10:15, j = 20:25)
)
)
to_ndjson( x = lst )
#> {"x":[1,2,3,4,5]}
#> {"y":{"a":["a","b","c","d","e"],"b":[{"i":10,"j":20},{"i":11,"j":21},{"i":12,"j":22},{"i":13,"j":23},{"i":14,"j":24},{"i":15,"j":25}]}}
to_ndjson( x = lst, by = "column")
#> {"x":[1,2,3,4,5]}
#> {"y":{"a":["a","b","c","d","e"],"b":{"i":[10,11,12,13,14,15],"j":[20,21,22,23,24,25]}}}