jsonfile.star¶
Read and edit any JSON file, keeping key order and indentation.
For files such as renovate.json, tsconfig.json without comments,
.eslintrc.json and composer.json. Keys are addressed with dots, such as
compilerOptions.strict; for a key that itself contains dots or slashes,
pass the path as a list, such as ["require", "laravel/framework"]. Files with comments (JSONC) can't be decoded and
fail the run.
| Function | What it does |
|---|---|
read |
Reads a JSON file into a value whose dict keys keep the file's order. |
write |
Writes a value as JSON in the layout the file already has. |
get |
Reads a value by dotted key path. |
set |
Sets a value by dotted key path, adding missing objects on the way. |
delete |
Removes a key by dotted key path. |
read¶
Reads a JSON file into a value whose dict keys keep the file's order.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
path |
The JSON file, relative to the repo root. |
default |
What to return when the file does not exist; without it, a missing file fails the run. |
Returns: The decoded value.
Example
write¶
Writes a value as JSON in the layout the file already has.
Values that didn't change keep their exact text, so a one-key edit is a one-line diff. Changed and new values follow the indentation and separators around them, and new lists of plain values are written on one line when the file already writes lists that way. A new file gets two spaces. Output ends with a newline.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
path |
The JSON file, relative to the repo root. |
data |
The value to write, usually from read. |
Returns: True when the file changed.
Example
config = read(repo, ".eslintrc.json")
config["extends"] = ["eslint:recommended"]
write(repo, ".eslintrc.json", config)
# returns True
get¶
Reads a value by dotted key path.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
path |
The JSON file, relative to the repo root. |
key |
The dotted key path, such as compilerOptions.strict. |
default |
What to return when the file or key is missing. |
Returns: The value, or default.
Example
set¶
Sets a value by dotted key path, adding missing objects on the way.
The file is created if needed. Existing keys keep their place; new keys go at the end of their object.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
path |
The JSON file, relative to the repo root. |
key |
The dotted key path, such as compilerOptions.strict. |
value |
Any JSON value: string, number, boolean, None, list or dict. |
Returns: True when the file changed.
Example
Used in: renovate-preset
delete¶
Removes a key by dotted key path.
Arguments
| Name | Description |
|---|---|
repo |
The repo passed to change(repo). |
path |
The JSON file, relative to the repo root. |
key |
The dotted key path. |
Returns: True when the file changed.
Example