Skip to content

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.

load("@fleet//jsonfile.star", "read", "write", "get", "set", "delete")
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

read(repo, path, default = None)

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

read(repo, "renovate.json")["extends"]
# returns ["config:base"]

renovate.json

{"extends": ["config:base"], "schedule": ["weekly"]}

Source

write

write(repo, path, data)

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

.eslintrc.json

{
    "root": true
}

.eslintrc.json

{
    "root": true,
    "extends": [
        "eslint:recommended"
    ]
}

Source

get

get(repo, path, key, default = None)

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

get(repo, "tsconfig.json", "compilerOptions.target")
# returns "ES2020"

tsconfig.json

{"compilerOptions": {"strict": false, "target": "ES2020"}}

Source

set

set(repo, path, key, value)

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

set(repo, "tsconfig.json", "compilerOptions.strict", True)
# returns True

tsconfig.json

{
  "compilerOptions": {
    "strict": false
  }
}

tsconfig.json

{
  "compilerOptions": {
    "strict": true
  }
}

Used in: renovate-preset

Source

delete

delete(repo, path, key)

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

delete(repo, "package.json", "scripts.postinstall")
# returns True

package.json

{
  "name": "web",
  "scripts": {
    "test": "jest",
    "postinstall": "husky install"
  }
}

package.json

{
  "name": "web",
  "scripts": {
    "test": "jest"
  }
}

Source