Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.
Worked example
import {
URL,
domainToASCII,
domainToUnicode,
pathToFileURL,
fileURLToPath,
} from "node:url";
console.log(URL.parse("broken url"));Result
nullImplementation notes
Distinguish parse failure from a valid null result. A fallback that uses null for both cases can make downstream error handling ambiguous.
Try it yourself
Try malformed text and valid JSON containing the wrong type.
Open the Node.js example →Editorial date November 14, 2026 · Published December 6, 2026