Remove Comments from JSON

Standard JSON does not support comments. If your JSON has // or /* */ comments, any standard JSON parser will throw a syntax error. Here's how to handle it.

Why JSON Doesn't Support Comments

JSON was intentionally designed without comment support by Douglas Crockford. His reasoning: JSON is a data interchange format, not a config file format. Comments were removed to prevent developers from using them to hold parsing directives (like XML processing instructions) that would differ between parsers.

In practice, developers constantly want comments in config files. This led to JSONC and JSON5 — two extensions of JSON that add comment support. Use the JSON Validator to verify whether your file is standard JSON or one of these extended formats.

This is NOT valid JSON

{
  // Database configuration
  "host": "localhost",
  "port": 5432, /* default postgres port */
  "name": "mydb"
}

Valid JSON (comments removed)

{
  "host": "localhost",
  "port": 5432,
  "name": "mydb"
}

How to Strip Comments — By Language

JavaScript / Node.js

// Option 1: strip-json-comments package
// npm install strip-json-comments
import stripJsonComments from 'strip-json-comments';

const jsonWithComments = `{
  // host config
  "host": "localhost",
  /* port */ "port": 5432
}`;

const cleanJson = stripJsonComments(jsonWithComments);
const config = JSON.parse(cleanJson);

// Option 2: VS Code / TypeScript - use jsonc-parser
// npm install jsonc-parser
import { parse } from 'jsonc-parser';
const config = parse(jsonWithComments); // handles JSONC natively

// Option 3: JSON5 for more flexible syntax
// npm install json5
import JSON5 from 'json5';
const config = JSON5.parse(jsonWithComments);

Python

import re
import json

def strip_comments(json_string):
    # Remove single-line comments
    json_string = re.sub(r'//.*', '', json_string)
    # Remove block comments
    json_string = re.sub(r'/*.*?*/', '', json_string, flags=re.DOTALL)
    return json_string

json_with_comments = """
{
  // this is a comment
  "host": "localhost",
  /* block comment */
  "port": 5432
}
"""

clean = strip_comments(json_with_comments)
config = json.loads(clean)

# Or use the commentjson package:
# pip install commentjson
import commentjson
config = commentjson.loads(json_with_comments)

C# — with Newtonsoft

using Newtonsoft.Json;

// Newtonsoft.Json supports comments natively
var settings = new JsonSerializerSettings
{
    // CommentHandling is Load by default — comments are allowed
};

string json = @"{
  // this comment is fine
  ""host"": ""localhost"",
  ""port"": 5432 /* block comment */
}";

var config = JsonConvert.DeserializeObject<Config>(json);
// Works! Newtonsoft strips comments during deserialization.

// System.Text.Json with JsonCommentHandling
var options = new System.Text.Json.JsonSerializerOptions
{
    ReadCommentHandling = System.Text.Json.JsonCommentHandling.Skip
};
var config2 = System.Text.Json.JsonSerializer.Deserialize<Config>(json, options);

JSONC vs JSON5 — Which to Use?

FeatureJSONCJSON5
File extension.jsonc.json5
// Comments✓ Yes✓ Yes
/* */ Block comments✓ Yes✓ Yes
Trailing commas✗ No✓ Yes
Unquoted keys✗ No✓ Yes
Single-quoted strings✗ No✓ Yes
Used byVS Code (settings.json, tsconfig)Config files, some build tools

FAQ

Does JSON support comments?expand_more

No. Standard JSON (RFC 8259) does not allow comments of any kind — not // line comments, not /* block comments */. If you need comments in a JSON-like format, use JSON5 or JSONC (JSON with Comments), which both extend JSON with comment support.

How do I remove comments from JSON before parsing?expand_more

Strip comments with a regex or a dedicated library before passing the string to JSON.parse(). In JavaScript: use the strip-json-comments package. In Node.js: use the json-parse-even-better-errors package. In VS Code, you can use the JSONC parser built into the editor.

What is JSONC?expand_more

JSONC (JSON with Comments) is a superset of JSON that allows single-line // comments and block /* */ comments. It is used by VS Code for configuration files (settings.json, tsconfig.json). JSONC files use the .jsonc extension or are parsed with JSONC-aware parsers.

What is JSON5?expand_more

JSON5 is a superset of JSON that adds comments, trailing commas, unquoted keys, single-quoted strings, and other JavaScript-inspired features. JSON5 files use the .json5 extension and require a JSON5 parser — they are not compatible with standard JSON.parse().

Remove Comments from JSON — Strip JSONC Before Parsing