Skip to main content
The json utility makes it easier to read and write to JSON files within in your checks. This utility expects an absolute path to a JSON file and returns an object with methods that help you read and write to any JSON file. To get full type-safety on all returned methods you can pass a generic type to the json function.

exists

Returns a boolean value indicating whether or not the file exists on disk.

Example


get

Returns

Returns the contents of a JSON file as an object. If the file does not exist or is not valid JSON, undefined will be returned.

Example


contains

Parameters

Record<string, unknown>
required
An object to check against the JSON file’s contents.

Returns

Returns a boolean value indicating whether or not the object is a subset of the JSON file’s contents. If the file does not exist or is not valid JSON, false will be returned.

Example


set

Overwrites the entire contents of a JSON file with the provided value. If the file does not exist, it will be created.

Parameters

Record<string, unknown>
required
An object that will be used to overwrite the JSON file’s contents.

Example


merge

Merges an object with the contents of a JSON file. If the file has the same keys as the passed-in object, the values for those keys will be overwritten. If the file does not exist, it will be created.

Parameters

Record<string, unknown>
required
An object that will be deeply merged with the JSON file’s contents.

Example


remove

Removes a property from a JSON file using a lodash style object path.

Parameters

string
required
A lodash-style path object that will be used to determine which property to remove.

Example


delete

Deletes a JSON file from disk.

Example