| jsonString | R Documentation |
R6 class to represent a JSON string.
prettyPrintget or set the value of prettyPrint
new()Creates a new jsonString object.
jsonString$new(string)
stringa string representing a JSON object, or the path to a JSON file
A jsonString object.
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$prettyPrint
jstring
jstring$prettyPrint <- FALSE
jstring
jstring <- "[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
jsonString$new(jstring)
print()Print a jsonString object.
jsonString$print(...)
...ignored
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring
jstring$prettyPrint <- FALSE
jstring
asString()Converts a jsonString to a character string.
jsonString$asString(pretty = FALSE)
prettyBoolean, whether to get a pretty string
A string.
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
cat(jstring$asString())
cat(jstring$asString(pretty = TRUE))
at()Extract an element in a JSON string by giving a path of keys or indices.
jsonString$at(...)
...the elements forming the path, integers or strings; an integer represents an index in an array (starting at 0), a string represents a key in an object
A jsonString object.
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$at(1)
jstring$at(2, "x")
hasKey()Checks whether a key exists in the reference JSON string.
jsonString$hasKey(key)
keya string
A Boolean value.
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$hasKey("x")
jstring <- jsonString$new(
"{\"x\": [2,3,4], \"y\": 42}"
)
jstring$hasKey("x")
keys()Get the keys of the reference JSON string (if it represents an object).
jsonString$keys()
A character vector.
jstring <- jsonString$new(
"{\"x\": [2,3,4], \"y\": 42}"
)
jstring$keys()
addProperty()Add a new property to the reference JSON string (if it represents an object).
jsonString$addProperty(key, value)
keya character string, the key of the new property
valuea JSON string, either a jsonString object or a
string
This updates the reference JSON string and this returns it invisibly.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
ppty <- jsonString$new("[9, 99]")
jstring$addProperty("c", ppty)
jstring
jstring$addProperty("d", "null")
jstring
erase()Erase an object property or an array element from the reference JSON string.
jsonString$erase(at)
ateither a character string, the key of the property to be erased, or an integer, the index of the array element to be erased
This invisibly returns the updated reference JSON string.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$erase("b")
jstring
jstring <- jsonString$new("[1, 2, 3, 4, 5]")
jstring$erase(2)
jstring
size()Number of elements in the reference JSON string.
jsonString$size()
An integer.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$size()
update()Update the reference JSON string (if it represents an object).
jsonString$update(jstring)
jstringa JSON string representing an object, either a
jsonString object or a string
This invisibly returns the updated reference JSON string.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring2 <- "{\"a\":[4,5,6],\"c\":\"goodbye\"}"
jstring$update(jstring2)
jstring
merge()Merge the reference JSON string (if it represents an object).
jsonString$merge(jstring)
jstringa JSON string, either a jsonString object or a
string representing a JSON object
This invisibly returns the updated reference JSON string.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring2 <- "{\"a\":[4,5,6],\"c\":\"goodbye\"}"
jstring$merge(jstring2)
jstring
patch()Apply a JSON patch to the reference JSON string (if it represents an array or an object).
jsonString$patch(jspatch)
jspatcha JSON patch, a JSON string representing an array (see
the link in details); it could be either a jsonString object or
a string
See jsonpatch.com.
A new jsonString object.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jspatch <- "[
{\"op\": \"remove\", \"path\": \"/a\"},
{\"op\": \"replace\", \"path\": \"/b\", \"value\": null}
]"
jstring$patch(jspatch)
push()Append an element to the reference JSON string (if it represents an array).
jsonString$push(jstring)
jstringa JSON string, either a jsonString object or a
string representing a JSON object
This invisibly returns the updated reference JSON string.
jstring <- jsonString$new("[1, 2, 3, 4, 5]")
jstring2 <- jsonString$new(
"{\"a\":[4,5,6],\"c\":\"goodbye\"}"
)
jstring$push(jstring2)
jstring
is()Check the type of the reference JSON string.
jsonString$is(type)
typethe type to be checked, one of "array", "object",
"string", "number", "integer", "float",
"null", "boolean"
A Boolean value.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$is("object")
jstring$is("array")
jstring <- jsonString$new("999")
jstring$is("integer")
jstring$is("number")
jstring$is("float")
type()Get the type of the reference JSON string.
jsonString$type()
A character string indicating the type of the JSON string.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$type()
jstring <- jsonString$new("999")
jstring$type()
flatten()Flatten the reference JSON string.
jsonString$flatten()
A new jsonString object.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":{\"x\":\"hello\",\"y\":\"hi\"}}"
)
jstring$flatten()
unflatten()Unflatten the reference JSON string (if it is flattened).
jsonString$unflatten()
A new jsonString object.
folder <- system.file(package = "jsonStrings")
files <- list.files(folder, recursive = TRUE)
sizes <- file.size(file.path(folder, files))
files <- sprintf('"%s"', paste0("/", files))
string <- sprintf("{%s}", paste0(files, ":", sizes, collapse = ","))
jstring <- jsonString$new(string)
jstring$unflatten()
writeFile()Write the reference JSON string to a file.
jsonString$writeFile(filename)
filenamename of the file
Nothing.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jsonfile <- tempfile(fileext = ".json")
jstring$writeFile(jsonfile)
cat(readLines(jsonfile), sep = "\n")
jsonString$new(jsonfile)
copy()Copy the reference JSON string.
jsonString$copy()
A new jsonString object.
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
copy <- jstring$copy()
copy$erase("b")
jstring
naive_copy <- jstring
naive_copy$erase("b")
jstring
## ------------------------------------------------
## Method `jsonString$new`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$prettyPrint
jstring
jstring$prettyPrint <- FALSE
jstring
jstring <- "[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
jsonString$new(jstring)
## ------------------------------------------------
## Method `jsonString$print`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring
jstring$prettyPrint <- FALSE
jstring
## ------------------------------------------------
## Method `jsonString$asString`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
cat(jstring$asString())
cat(jstring$asString(pretty = TRUE))
## ------------------------------------------------
## Method `jsonString$at`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$at(1)
jstring$at(2, "x")
## ------------------------------------------------
## Method `jsonString$hasKey`
## ------------------------------------------------
jstring <- jsonString$new(
"[1, [\"a\", 99], {\"x\": [2,3,4], \"y\": 42}]"
)
jstring$hasKey("x")
jstring <- jsonString$new(
"{\"x\": [2,3,4], \"y\": 42}"
)
jstring$hasKey("x")
## ------------------------------------------------
## Method `jsonString$keys`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"x\": [2,3,4], \"y\": 42}"
)
jstring$keys()
## ------------------------------------------------
## Method `jsonString$addProperty`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
ppty <- jsonString$new("[9, 99]")
jstring$addProperty("c", ppty)
jstring
jstring$addProperty("d", "null")
jstring
## ------------------------------------------------
## Method `jsonString$erase`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$erase("b")
jstring
jstring <- jsonString$new("[1, 2, 3, 4, 5]")
jstring$erase(2)
jstring
## ------------------------------------------------
## Method `jsonString$size`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$size()
## ------------------------------------------------
## Method `jsonString$update`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring2 <- "{\"a\":[4,5,6],\"c\":\"goodbye\"}"
jstring$update(jstring2)
jstring
## ------------------------------------------------
## Method `jsonString$merge`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring2 <- "{\"a\":[4,5,6],\"c\":\"goodbye\"}"
jstring$merge(jstring2)
jstring
## ------------------------------------------------
## Method `jsonString$patch`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jspatch <- "[
{\"op\": \"remove\", \"path\": \"/a\"},
{\"op\": \"replace\", \"path\": \"/b\", \"value\": null}
]"
jstring$patch(jspatch)
## ------------------------------------------------
## Method `jsonString$push`
## ------------------------------------------------
jstring <- jsonString$new("[1, 2, 3, 4, 5]")
jstring2 <- jsonString$new(
"{\"a\":[4,5,6],\"c\":\"goodbye\"}"
)
jstring$push(jstring2)
jstring
## ------------------------------------------------
## Method `jsonString$is`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$is("object")
jstring$is("array")
jstring <- jsonString$new("999")
jstring$is("integer")
jstring$is("number")
jstring$is("float")
## ------------------------------------------------
## Method `jsonString$type`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jstring$type()
jstring <- jsonString$new("999")
jstring$type()
## ------------------------------------------------
## Method `jsonString$flatten`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":{\"x\":\"hello\",\"y\":\"hi\"}}"
)
jstring$flatten()
## ------------------------------------------------
## Method `jsonString$unflatten`
## ------------------------------------------------
folder <- system.file(package = "jsonStrings")
files <- list.files(folder, recursive = TRUE)
sizes <- file.size(file.path(folder, files))
files <- sprintf('"%s"', paste0("/", files))
string <- sprintf("{%s}", paste0(files, ":", sizes, collapse = ","))
jstring <- jsonString$new(string)
jstring$unflatten()
## ------------------------------------------------
## Method `jsonString$writeFile`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
jsonfile <- tempfile(fileext = ".json")
jstring$writeFile(jsonfile)
cat(readLines(jsonfile), sep = "\n")
jsonString$new(jsonfile)
## ------------------------------------------------
## Method `jsonString$copy`
## ------------------------------------------------
jstring <- jsonString$new(
"{\"a\":[1,2,3],\"b\":\"hello\"}"
)
copy <- jstring$copy()
copy$erase("b")
jstring
naive_copy <- jstring
naive_copy$erase("b")
jstring
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.