Skip to content

gaborcsardi/tsjsonc

Repository files navigation

tsjsonc

lifecycle R-CMD-check Codecov test coverage

Extract and manipulate parts of JSON files without touching the formatting and comments in other parts.

Installation

You can install the development version of tsjsonc from GitHub with:

# install.packages("pak")
pak::pak("gaborcsardi/tsjsonc")

Documentation

See at https://gaborcsardi.github.io/tsjsonc/ and also in the installed package: help(package = "tsjsonc").

Quickstart

Create a tsjsonc object

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:

json

Select elements in a tsjsonc object

Select 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 elements

Delete selected elements:

ts_tree_select(json, "a", "a1") |> ts_tree_delete()

Insert elements

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 elements

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 } }")
json
ts_tree_select(json, "a", "x", "y") |> ts_tree_update(list(1,2,3))

License

MIT © Posit Software, PBC

About

Edit JSON files

Topics

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors