Description Usage Arguments Value Examples
Extract an element at a given path.
1 2 3 4 5 6 7 8 9 | json_extract(
x,
path,
ptype = NULL,
default = NULL,
na = NA,
wrap_scalars = FALSE,
bigint_as_char = bigint_default()
)
|
x |
A JSON vector. |
path |
Path to element. This must be a valid
JSONpath expression.
For example |
ptype |
Output type. If |
default |
Default value if path doesn't exist or element at path is empty. |
na |
Default value if element of |
wrap_scalars |
Should scalar values be wrapped? Note that scalars are only wrapped if either
|
bigint_as_char |
Convert big integers to character? The option
|
A vector with class given by ptype
and length equal to x
. Mind
that for new_json_array()
and new_json_object()
the return type will
only be json2
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | x1 <- '{"a": 1, "b": 2}'
json_extract(x1, "$.a")
json_extract('{"a": {"b": 1}}', "$.a")
# `NA` values stay `NA` ...
json_extract(c(NA_character_, x1), "$.a")
# ... but can return the value of `na` instead.
json_extract(c(NA_character_, x1), "$.a", na = 3)
# missing paths error by default ...
try(json_extract(x1, "$.c"))
# ... but can be replaced by the value of `default` instead.
json_extract(x1, "$.c", default = "not there")
# make sure to error if you don't get back an array
json_extract('{"a": [1]}', "$.a", ptype = new_json_array())
try(json_extract('{"a": {"b": 1}}', "$.a", ptype = new_json_array()))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.