Skip to content

Function: parse()

parse<T>(text, fallback): T

Parse JSON text, returning fallback (default undefined) instead of throwing when text is absent or malformed.

The return type is caller-asserted, exactly like JSON.parse - this only removes the try/catch, it does not validate. Prefer parseRecord or a schema when the result is indexed.

T = unknown

unknown

T

T

const config = json.parse<Config>(await readFile(path, "utf8"));
const tags = json.parse(process.env.TAGS, []);

parse<T>(text): T | undefined

Parse JSON text, returning fallback (default undefined) instead of throwing when text is absent or malformed.

The return type is caller-asserted, exactly like JSON.parse - this only removes the try/catch, it does not validate. Prefer parseRecord or a schema when the result is indexed.

T = unknown

unknown

T | undefined

const config = json.parse<Config>(await readFile(path, "utf8"));
const tags = json.parse(process.env.TAGS, []);