Skip to content
JSON Tidy

JSONC to JSON: remove comments

Paste a tsconfig.json, a VS Code settings file, or any other JSON with comments, and get plain JSON that JSON.parse and other strict parsers accept. Comments and trailing commas are removed, and everything else stays as written. The conversion runs in your browser, so nothing is uploaded.

JSON with comments

Plain JSON

Can JSON have comments?

No. The JSON standard, RFC 8259, has no syntax for comments, so a strict parser stops at the first // or /*. The JSON format guide covers why they were left out.

The error messages don’t mention comments, which makes them hard to trace. These are the messages for a comment after a value, {"port": 8080 // dev}, and a comment on its own line before a key:

ParserComment after a valueComment before a key
JavaScript (Chrome, Node.js)Expected ',' or '}' after property value in JSON at position 14Expected property name or '}' in JSON at position 4
PythonExpecting ',' delimiter: line 1 column 15 (char 14)Expecting property name enclosed in double quotes: line 2 column 3 (char 4)

The second one is covered in more detail in Expecting property name enclosed in double quotes.

Which files use JSON with comments

JSONC (JSON with comments) is JSON plus // and /* */ comments. Most tools that accept it allow trailing commas too. You’ll find it in:

  • tsconfig.json and jsconfig.json
  • VS Code’s settings.json, keybindings.json, launch.json, and tasks.json
  • devcontainer.json

The tools that own these files read them with a JSONC parser, so the comments are fine there. The trouble starts when something else reads the file. A build script that runs JSON.parse(fs.readFileSync("tsconfig.json", "utf8")) fails with one of the errors above. So does copying settings into a file that has to be strict JSON. package.json is one: npm can’t read it if it has a comment.

What the converter changes

Choose Example to load a tsconfig.json with three comments and three trailing commas. The converter removes:

  • // comments, along with the spaces before them
  • /* */ comments, including ones that span several lines
  • Lines that only held a comment, so no blank gaps are left behind
  • Commas right before a } or ]

Text inside strings is never touched. That matters more than it seems, because the usual shortcut is a find-and-replace:

// Looks reasonable, but it breaks the $schema line:
text.replace(/\/\/.*$/gm, "")

// "$schema": "https://json.schemastore.org/tsconfig",
// becomes
// "$schema": "https:

Replacing /* */ the same way has a similar problem with the example’s "@/*" path: it would delete everything from there to the end of the next comment. The converter reads the file the way a parser does, so it knows when it’s inside a string. Indentation, key order, and numbers are left exactly as they were.

Single quotes and unquoted keys aren’t part of JSONC. They belong to JSON5 and to JavaScript objects. If the result still isn’t valid JSON, the tool tells you, and JSON repair can fix those too.

Reading JSONC in code

If a script reads a JSONC file, use a parser that accepts comments instead of converting the file by hand each time:

// JavaScript (npm install jsonc-parser)
import { parse } from "jsonc-parser";
const errors = [];
const config = parse(text, errors, { allowTrailingComma: true });
if (errors.length) throw new Error("tsconfig.json has a syntax error");

# Python (pip install json5)
import json5
config = json5.loads(text)

// Go (go get github.com/tailscale/hujson)
standard, err := hujson.Standardize(data)
if err == nil {
    err = json.Unmarshal(standard, &config)
}

jsonc-parser is the parser VS Code uses. JSON5 is a superset of JSONC, so a JSON5 parser reads any JSONC file.

Leaving notes in strict JSON

If a file has to stay plain JSON, put the note in the data. Most programs ignore keys they don’t know about, so a key like "_comment" or "//" works in many config files, including package.json. Check first if the file is validated against a schema that doesn’t allow extra properties. If you control the format and want real comments, YAML has them: the JSON to YAML converter turns a file over.

Files can be up to 2 MB. Download keeps the original file name, so tsconfig.jsonc comes back as tsconfig.json.