JSON5 to JSON Converter · Tutorial
How to convert tsconfig.json with comments to strict JSON
5 min readbeginnerUpdated
Files like tsconfig.json and VS Code's settings.json allow comments and trailing commas, so a script that reads them with JSON.parse fails with an "unexpected token" error. This tutorial shows how to convert such a file into strict JSON in seconds, and how to avoid the problem in your own scripts.
What you need
- The JSON5 to JSON converter
- A JSONC file such as tsconfig.json (the Try it button loads one)
Steps
Open the file
Drop your tsconfig.json onto Open a .json5 or .jsonc file, or paste its contents into the left box. The sample has a
//comment at the top, a/* Emit */block comment, and trailing commas aftersourceMapand the closing brace.Check the strict JSON output
The right box shows the same settings as strict JSON straight away. Both comments are gone and the trailing commas are removed. Everything else, including the order of the options, is unchanged.
{ "compilerOptions": { "target": "ES2022", "module": "ESNext", "strict": true, "outDir": "./dist", "sourceMap": true }, "include": ["src"] }Choose the format you need
Use 2 spaces to match most projects, or Minified if the JSON goes into a single-line field or environment variable.
Copy or download
Press Copy, or Download .json to save the converted file. Keep your original tsconfig.json with its comments for TypeScript, and use the converted copy wherever strict JSON is required.
Avoid the problem in scripts
If you read these files from code, use a parser that understands comments instead of converting each time. In Node.js, the
json5package parses them directly:JSON5.parse(fs.readFileSync('tsconfig.json', 'utf8')). TypeScript itself also offersts.readConfigFile.
Try it yourself
Opens the JSON5 to JSON Converter with this tutorial's sample loaded.
Common mistakes
- Replacing the original file
- TypeScript and VS Code are happy with comments, and the comments explain your settings. Don't overwrite the original with the stripped version unless a tool really requires it.
- Deleting comments with a search and replace
- A regex that removes
//can also break strings that contain URLs, like"https://...". A real parser knows the difference between a comment and text inside a string. - Forgetting the extends chain
- A tsconfig.json can
extendanother file. Converting one file doesn't merge in the settings it inherits.
Next steps
Go back to the JSON5 to JSON Converter, or keep going with one of these:
FAQ
Will TypeScript still read the converted file?
Yes. Strict JSON is valid JSONC, so TypeScript reads it fine. It just loses the comments.