Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

tojson

A simple JSON serializer


Description

A JSON serializer that only works on a limited types of R data (NULL, lists, logical scalars, character/numeric vectors). A character string of the class JS_EVAL is treated as raw JavaScript, so will not be quoted. The function json_vector() converts an atomic R vector to JSON.

Usage

tojson(x)

json_vector(x, to_array = FALSE, quote = TRUE)

Arguments

x

An R object.

to_array

Whether to convert a vector to a JSON array (use []).

quote

Whether to double quote the elements.

Value

A character string.

See Also

The jsonlite package provides a full JSON serializer.

Examples

library(xfun)
tojson(NULL)
tojson(1:10)
tojson(TRUE)
tojson(FALSE)
cat(tojson(list(a = 1, b = list(c = 1:3, d = "abc"))))
cat(tojson(list(c("a", "b"), 1:5, TRUE)))

# the class JS_EVAL is originally from htmlwidgets::JS()
JS = function(x) structure(x, class = "JS_EVAL")
cat(tojson(list(a = 1:5, b = JS("function() {return true;}"))))

xfun

Miscellaneous Functions to Support Packages Maintained by 'Yihui Xie'

v0.22
MIT + file LICENSE
Authors
Yihui Xie [aut, cre, cph] (<https://orcid.org/0000-0003-0645-5666>), Wush Wu [ctb], Daijiang Li [ctb], Xianying Tan [ctb], Salim Brüggemann [ctb] (<https://orcid.org/0000-0002-5329-5987>), Christophe Dervieux [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.