Learn R Programming

hypothesis (version 1.1.0)

as_json: Convert character string to JSON

Description

Allows to include raw JS code in JSON object.

Usage

as_json(x)

Value

A character of class 'json'. The output can be used to store bare JS element withing JSON object.

Arguments

x

Character storing JS code or object.

Details

Along with json_verbatim = TRUE argument of toJSON makes passing the code to JSON object possible.

Examples

Run this code
# Function stored as text
jsonlite::toJSON(
  list(fun = "function(x) {console.log(x);}"),
  auto_unbox = TRUE, json_verbatim = TRUE
)
# Function stored as raw JS object
jsonlite::toJSON(
  list(fun = as_json("function(x) {console.log(x);}")),
  auto_unbox = TRUE, json_verbatim = TRUE
)

Run the code above in your browser using DataLab