Title: | JSON for R |
---|---|
Description: | Converts R object into JSON objects and vice-versa. |
Authors: | Alex Couture-Beil [aut, cre] |
Maintainer: | Alex Couture-Beil <[email protected]> |
License: | GPL-2 |
Version: | 0.2.23 |
Built: | 2024-11-15 06:17:54 UTC |
Source: | https://github.com/alexcb/rjson |
Convert a JSON object into an R object.
fromJSON( json_str, file, method = "C", unexpected.escape = "error", simplify = TRUE )
fromJSON( json_str, file, method = "C", unexpected.escape = "error", simplify = TRUE )
json_str |
a JSON object to convert |
file |
the name of a file to read the json_str from; this can also be a URL. Only one of json_str or file must be supplied. |
method |
use the |
unexpected.escape |
changed handling of unexpected escaped characters. Handling value should be one of "error", "skip", or "keep"; on unexpected characters issue an |
simplify |
If TRUE, attempt to convert json-encoded lists into vectors where appropriate. If FALSE, all json-encoded lists will be wrapped in a list even if they are all of the same data type. |
R object that corresponds to the JSON object
fromJSON('[1,2,3]', simplify=TRUE) # returns c(1,2,3) fromJSON('[1,2,3]', simplify=FALSE) # returns list(1,2,3) #As a result, this will output "1" toJSON(fromJSON('[1]', simplify=TRUE)) #Compared with this which will output "[1]" as expected toJSON(fromJSON('[1]', simplify=FALSE)) #R vs C execution time x <- toJSON( iris ) system.time( y <- fromJSON(x) ) system.time( y2 <- fromJSON(x,method = "R") )
fromJSON('[1,2,3]', simplify=TRUE) # returns c(1,2,3) fromJSON('[1,2,3]', simplify=FALSE) # returns list(1,2,3) #As a result, this will output "1" toJSON(fromJSON('[1]', simplify=TRUE)) #Compared with this which will output "[1]" as expected toJSON(fromJSON('[1]', simplify=FALSE)) #R vs C execution time x <- toJSON( iris ) system.time( y <- fromJSON(x) ) system.time( y2 <- fromJSON(x,method = "R") )
Convert a collection of JSON objects into R objects.
newJSONParser(method = "R")
newJSONParser(method = "R")
method |
use the |
A list of functions used for parsing objects
sample_json <- ' { "breakfast" : [ "milk", "fruit loops", "juice" ], "lunch" : [ "left over sushi" ] } ' parser <- newJSONParser() parser$addData( sample_json ) food <- parser$getObject() print( food ) #This is equivalent to using FromJSON( sample_json ) #However, sample_json can be split into several parts: ### EXAMPLE 2: part_1 <- '{ "breakfast" : [ "milk", "fruit loops", "juice" ], ' part_2 <- '"lunch" : [ "left over sushi" ]' # close off the first object, and create a 2nd JSON object, which is simply an # array part_3 <- '} [1,2,3,4,5]' parser <- newJSONParser() parser$addData( part_1 ) parser$getObject() #returns NULL - since part_1 isn't complete parser$addData( part_2 ) parser$getObject() #returns NULL - since part_2 still isn't complete parser$addData( part_3 ) parser$getObject() #returns the first food object parser$getObject() #returns the second array
sample_json <- ' { "breakfast" : [ "milk", "fruit loops", "juice" ], "lunch" : [ "left over sushi" ] } ' parser <- newJSONParser() parser$addData( sample_json ) food <- parser$getObject() print( food ) #This is equivalent to using FromJSON( sample_json ) #However, sample_json can be split into several parts: ### EXAMPLE 2: part_1 <- '{ "breakfast" : [ "milk", "fruit loops", "juice" ], ' part_2 <- '"lunch" : [ "left over sushi" ]' # close off the first object, and create a 2nd JSON object, which is simply an # array part_3 <- '} [1,2,3,4,5]' parser <- newJSONParser() parser$addData( part_1 ) parser$getObject() #returns NULL - since part_1 isn't complete parser$addData( part_2 ) parser$getObject() #returns NULL - since part_2 still isn't complete parser$addData( part_3 ) parser$getObject() #returns the first food object parser$getObject() #returns the second array
JSON (JavaScript Object Notation) is a lightweight data-interchange format. This package converts JSON objects into R objects and vice-versa. See json.org for an overview of JSON. Unicode is unfortunately not supported at the moment.
Convert an R object into a corresponding JSON object.
***Lists with unnamed components are not currently supported***
toJSON( x, indent=0, method="C" )
toJSON( x, indent=0, method="C" )
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 |
a string containing the JSON object
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);
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);