toJSON {JSON}R Documentation

Converts R object to the JSON format

Description

toJSON takes an R object and attempt to convert it to the JSON format.

Usage

toJSON(x, scalars = TRUE)

Arguments

x

R object to convert.

scalars

logical. If set to TRUE then vectors of length one are treated as scalars in JSON. Otherwise vectors are always lists.

Details

toJSON attempts to convert R object to the JSON format. However, the JSON format only supports lists, maps, strings, numbers and booleans. This means that the conversion can be lossy. toJSON uses the following conventions: named vectors are stored as maps unless the names are non-unique. Matrices and arrays are stored as recursive lists such that accessing array[x,y,z] in R is equivalent to array[x][y][z] in JSON. Only numeric and logical vectors are treated separately, all other obejcts are coerced into strings using the as.character() method.

Value

A character vector of length one containing the JSON encoding of the R obejct.

Author(s)

Simon Urbanek

Examples

  toJSON(c("foo","bar"))
  data(iris)
  toJSON(iris[1:5,])
  toJSON(list(m=matrix(1:6,2)))

[Package JSON version 0.1-0 Index]