Remove comments from JSON

Got a config file with // or /* */ notes in it that a strict JSON parser chokes on? Paste it here and get back something a plain JSON.parse will accept.

JSON
With comments
Loading editor…
Cleaned
Cleaned JSON will appear here.

JSON doesn't actually allow comments

This trips people up constantly: the JSON spec has no comment syntax, full stop. What you're usually holding is JSONC, the format VS Code uses for settings.json and tsconfig.json, or JSON5, which is a bit looser still. Both add // and /* */ comments on top of JSON. Editors that know about these formats handle them fine. A plain JSON.parse call in Node, or almost any other language's JSON library, does not, and throws a syntax error on the first comment it hits.

Stripping the comments out first gets you back to something every JSON parser on earth can read, without hand-editing the file line by line.

What this does and doesn't handle

Both comment styles come out: a // that runs to the end of a line, and a /* */ block that can span several. Anything inside a string value is left alone. What this pass doesn't fix yet is trailing commas, which JSON5 also permits and strict JSON doesn't, so a file that leans on both features may still need a second pass by hand.

Frequently asked questions

Wait, doesn't JSON not support comments?

Right, strict JSON has no comment syntax at all. What people usually mean is JSONC (used by VS Code's settings.json and tsconfig.json) or JSON5, both of which allow // and /* */ as an extension. This tool strips those out so the result parses with a plain JSON.parse.

Will it touch a // or /* that shows up inside a string value?

No. Only comments outside of string values are removed. A URL or file path stored as a JSON string is left exactly as written.

Does it also remove trailing commas?

Not yet, this pass is comments only. JSON5 also allows trailing commas, which strict JSON doesn't, so you may still need to clean those up by hand for now.