Extract and manipulate parts of JSON files without touching the formatting and comments in other parts.
You can install the development version of tsjsonc from GitHub with:
# install.packages("pak")
pak::pak("gaborcsardi/tsjsonc")See at
https://gaborcsardi.github.io/tsjsonc/
and also in the installed package: help(package = "tsjsonc").
Create a tsjsonc object from a string:
txt <- r"(
// this is a comment
{
"a": {
"a1": [1, 2, 3],
// comment
"a2": "string"
},
"b": [
{
"b11": true,
"b12": false
},
{
"b21": false,
"b22": false
}
]
}
)"
json <- ts_parse_jsonc(text = txt)Pretty print a tsjsonc object:
jsonSelect element by objects key:
ts_tree_select(json, "a")Select element inside element:
ts_tree_select(json, "a", "a1")Select element(s) of an array:
ts_tree_select(json, "a", "a1", 1:2)Select multiple keys from an object:
ts_tree_select(json, "a", c("a1", "a2"))Select nodes that match a tree-sitter query:
json |> ts_tree_select(query = "((pair value: (false) @val))")Delete selected elements:
ts_tree_select(json, "a", "a1") |> ts_tree_delete()Insert element into an array:
ts_tree_select(json, "a", "a1") |> ts_tree_insert(at = 2, "new")Inserting into an array reformats the array.
Insert element into an object, at the specified key:
ts_tree_select(json, "a") |>
ts_tree_insert(key = "a0", at = 0, list("new", "element"))Update existing element:
ts_tree_select(json, "a", c("a1", "a2")) |> ts_tree_update("new value")Inserts the element if some parents are missing:
json <- ts_parse_jsonc(text = "{ \"a\": { \"b\": true } }")
jsonts_tree_select(json, "a", "x", "y") |> ts_tree_update(list(1,2,3))MIT © Posit Software, PBC