toJSON: Convert R To JSON

Description Usage Arguments Value See Also Examples

View source: R/json.R

Description

Convert an R object into a corresponding JSON object.

***Lists with unnamed components are not currently supported***

Usage

1
toJSON( x, indent=0, method="C" )

Arguments

x

a vector or list to convert into a JSON object

indent

an integer specifying how much indentation to use when formatting the JSON object; if 0, no pretty-formatting is used

method

use the C implementation, or the older slower (and one day to be depricated) R implementation

Value

a string containing the JSON object

See Also

fromJSON

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
x <- list( alpha = 1:5, beta = "Bravo", 
           gamma = list(a=1:3, b=NULL), 
           delta = c(TRUE, FALSE) )
json <- toJSON( x )
fromJSON( json )


#named vectors are treated as JSON objects (lists)
toJSON(islands[1:4])


#data.frames must be converted into a list before converting into JSON
plot(cars, pch=2)
json_cars <- toJSON(as.list(cars))
points( data.frame( fromJSON( json_cars ) ), col="red", pch=3 )

#special R types are encoded as strings
testString <- c(1,2,3,4,NA,NaN,Inf,8,9);
toJSON(testString);

rjson documentation built on Jan. 9, 2022, 9:07 a.m.