safeParse
Parses JSON permissively like parse — strict JSON, JSONC (HuJSON, JWCC) and JSON5 —
but returns null (or fallback) instead of throwing on invalid input.
Accepts / rejects: exactly what parse does — see there for the full list.
On invalid input: returns fallback when one is given — even undefined, which is told
apart from an omitted argument — and null otherwise. Never throws.
Available since v3.1.3
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
text | string | The JSON, JSONC or JSON5 text to parse |
Returns
Section titled “Returns”T | null — The parsed value typed as T, or null on invalid input (pass a fallback to get it instead)
Examples
Section titled “Examples”Read JSONC or JSON5 without throwing
Section titled “Read JSONC or JSON5 without throwing”Same parsing as parse, but returns null on invalid input.
Fall back to defaults
Section titled “Fall back to defaults”Pass a fallback to get a usable value even when the file is broken.
