Title: | JSON Parsing and Modification Utilities |
---|---|
Description: | Bindings to 'node-jsonc-parser' to parse, format, and modify JSON files while retaining comments. |
Authors: | Davis Vaughan [aut, cre], Jeroen Ooms [aut], Microsoft Corporation [cph] (node-jsonc-parser library) |
Maintainer: | Davis Vaughan <davis@posit.co> |
License: | MIT + file LICENSE |
Version: | 0.1.0 |
Built: | 2025-09-29 09:27:32 UTC |
Source: | https://github.com/r-lib/jsonedit |
Format a JSON file or string, preserving comments.
text_format(text, ..., formatting_options = NULL) file_format(file, ..., formatting_options = NULL) formatting_options( indent_width = 4L, indent_style = "space", eol = "\n", insert_final_newline = TRUE )
text_format(text, ..., formatting_options = NULL) file_format(file, ..., formatting_options = NULL) formatting_options( indent_width = 4L, indent_style = "space", eol = "\n", insert_final_newline = TRUE )
text |
A single string containing JSON. |
... |
These dots are for future extensions and must be empty. |
formatting_options |
The result of |
file |
Path to file on disk. File must exist. |
indent_width |
The number of spaces to use to indicate a single indent
when |
indent_style |
The style of indentation to use. Either:
|
eol |
The character used for the end of a line. This is only applicable when the text doesn't already contain an existing line ending, i.e. an empty string or a string spanning a single line. |
insert_final_newline |
Whether or not to insert a final newline. |
text <- '{"foo":[1,2]}' cat(text_format(text)) formatting_options <- formatting_options(indent_width = 2) cat(text_format(text, formatting_options = formatting_options))
text <- '{"foo":[1,2]}' cat(text_format(text)) formatting_options <- formatting_options(indent_width = 2) cat(text_format(text, formatting_options = formatting_options))
Set or delete fields in a JSON file or string while retaining comments and whitespace.
text_modify( text, path, value, ..., parse_options = NULL, modification_options = NULL ) file_modify( file, path, value, ..., parse_options = NULL, modification_options = NULL ) modification_options(formatting_options = NULL, is_array_insertion = FALSE)
text_modify( text, path, value, ..., parse_options = NULL, modification_options = NULL ) file_modify( file, path, value, ..., parse_options = NULL, modification_options = NULL ) modification_options(formatting_options = NULL, is_array_insertion = FALSE)
text |
A single string containing JSON to modify. |
path |
Either:
Numeric positions are specified as positive integers and are only
applicable for arrays. |
value |
New value. Wrap in |
... |
These dots are for future extensions and must be empty. |
parse_options |
The result of |
modification_options |
The result of |
file |
Path to file on disk. File must exist. |
formatting_options |
The result of a call to |
is_array_insertion |
Whether or not to treat the change as an
insertion at the specified |
text <- "{}" text <- text_modify(text, c('[r]', 'editor.formatOnSave'), TRUE) cat(text) text <- text_modify(text, c('[r]', 'editor.formatOnSave'), NULL) cat(text) # Insert an array text <- text_modify(text, "foo", 1:3) cat(text) # Update the array at location 2 cat(text_modify(text, list("foo", 2), 0)) # Insert at location 2 cat(text_modify( text, list("foo", 2), 0, modification_options = modification_options(is_array_insertion = TRUE) )) # Insert at the end of the array. `-1` is treated as an insertion regardless # of the value of `is_array_insertion`. cat(text_modify(text, list("foo", -1), 0)) # Only the modified elements are reformatted text <- '{"foo":[1,2],\n"bar":1}' cat(text_modify(text, list("foo", 3), 0)) # You can control how those elements are formatted cat(text_modify( text, list("foo", 3), 0, modification_options = modification_options( formatting_options = formatting_options(indent_width = 2), is_array_insertion = TRUE ) ))
text <- "{}" text <- text_modify(text, c('[r]', 'editor.formatOnSave'), TRUE) cat(text) text <- text_modify(text, c('[r]', 'editor.formatOnSave'), NULL) cat(text) # Insert an array text <- text_modify(text, "foo", 1:3) cat(text) # Update the array at location 2 cat(text_modify(text, list("foo", 2), 0)) # Insert at location 2 cat(text_modify( text, list("foo", 2), 0, modification_options = modification_options(is_array_insertion = TRUE) )) # Insert at the end of the array. `-1` is treated as an insertion regardless # of the value of `is_array_insertion`. cat(text_modify(text, list("foo", -1), 0)) # Only the modified elements are reformatted text <- '{"foo":[1,2],\n"bar":1}' cat(text_modify(text, list("foo", 3), 0)) # You can control how those elements are formatted cat(text_modify( text, list("foo", 3), 0, modification_options = modification_options( formatting_options = formatting_options(indent_width = 2), is_array_insertion = TRUE ) ))
text_parse()
and file_parse()
parse JSON into an R object.
text_parse_at_path()
and file_parse_at_path()
parse JSON at a requested
JSON path, i.e. c("[r]", "editor.formatOnSave")
.
text_parse(text, ..., parse_options = NULL) file_parse(file, ..., parse_options = NULL) text_parse_at_path(text, path, ..., parse_options = NULL) file_parse_at_path(file, path, ..., parse_options = NULL) parse_options( allow_comments = TRUE, allow_trailing_comma = TRUE, allow_empty_content = TRUE )
text_parse(text, ..., parse_options = NULL) file_parse(file, ..., parse_options = NULL) text_parse_at_path(text, path, ..., parse_options = NULL) file_parse_at_path(file, path, ..., parse_options = NULL) parse_options( allow_comments = TRUE, allow_trailing_comma = TRUE, allow_empty_content = TRUE )
text |
A single string containing JSON. |
... |
These dots are for future extensions and must be empty. |
parse_options |
The result of |
file |
Path to file on disk. File must exist. |
path |
Either:
Numeric positions are specified as positive integers and are only applicable for arrays. |
allow_comments |
Whether or not to allow comments when parsing. |
allow_trailing_comma |
Whether or not to allow a trailing comma when parsing. |
allow_empty_content |
Whether or not to allow empty strings or empty files when parsing. |
text <- ' { "a": 1, "b": [2, 3, 4], "[r]": { "this": "setting", // A comment! "that": true }, // A trailing comma! } ' # Parse the JSON, allowing comments (i.e. JSONC) str(text_parse(text)) # Try to parse the JSON, but comments aren't allowed! parse_options <- parse_options(allow_comments = FALSE) try(text_parse(text, parse_options = parse_options)) # Try to parse the JSON, but trailing commas aren't allowed! parse_options <- parse_options(allow_trailing_comma = FALSE) try(text_parse(text, parse_options = parse_options)) # Parse only a subset of the JSON text_parse_at_path(text, "b") text_parse_at_path(text, "[r]") text_parse_at_path(text, c("[r]", "that")) # Use a `list()` combining strings and positional indices when # arrays are involved text_parse_at_path(text, list("b", 2))
text <- ' { "a": 1, "b": [2, 3, 4], "[r]": { "this": "setting", // A comment! "that": true }, // A trailing comma! } ' # Parse the JSON, allowing comments (i.e. JSONC) str(text_parse(text)) # Try to parse the JSON, but comments aren't allowed! parse_options <- parse_options(allow_comments = FALSE) try(text_parse(text, parse_options = parse_options)) # Try to parse the JSON, but trailing commas aren't allowed! parse_options <- parse_options(allow_trailing_comma = FALSE) try(text_parse(text, parse_options = parse_options)) # Parse only a subset of the JSON text_parse_at_path(text, "b") text_parse_at_path(text, "[r]") text_parse_at_path(text, c("[r]", "that")) # Use a `list()` combining strings and positional indices when # arrays are involved text_parse_at_path(text, list("b", 2))