parse
Parses JSON permissively: strict JSON, JSONC (the dialect of tsconfig.json and VS Code’s
settings.json, also known as HuJSON or JWCC) and JSON5 — each a superset of the one before.
Accepts:
- everything parseStrict accepts (strict JSON, RFC 8259)
- comments:
// …to the end of the line, and/* … */ - one trailing comma after the last item of an object or array
- object keys without quotes, when they are ECMAScript identifiers (
{ name: 1 },{ $id: 1 }) - strings in single quotes, and in either quote: the escapes
\',\v,\0and\xHH, any other escaped character standing for itself, and a backslash before a line break to continue the string on the next line - numbers with a leading
+, a leading or trailing dot (.5,5.), in hexadecimal (0x1F), andInfinity,-InfinityandNaN - any Unicode whitespace between tokens, including a leading byte order mark (U+FEFF)
Rejects: more than one trailing comma in a row, leading zeros (01), octal or binary
numbers, unquoted keys that aren’t identifiers ({ my-key: 1 }), raw line breaks inside a
string, an unterminated comment or string, undefined, and more than one value.
On invalid input: throws a SyntaxError naming the line and column of the first problem.
Strict JSON goes straight to the native JSON.parse, and JSONC to JSON.parse after its
comments are blanked out; only text that is neither goes through the JSON5 reader (written for
helpers4 from the JSON5 1.0.0 specification), so the common cases cost nothing extra.
Values are built as JSON.parse builds them: the last of duplicate keys wins, and a
__proto__ key is an own property, never the prototype. NaN and Infinity have no JSON
representation — stringify writes them as null.
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 — The parsed value, typed as T
Examples
Section titled “Examples”Parse a config file with comments
Section titled “Parse a config file with comments”Reads JSONC — the tsconfig.json / settings.json dialect — as well as strict JSON.
Invalid input throws
Section titled “Invalid input throws”The SyntaxError names the line and column of the problem.
Parse JSON5
Section titled “Parse JSON5”Unquoted keys, single quotes, hexadecimal and leading-dot numbers, trailing commas.
