Lineserve

The Definitive Guide to Using Comments in JSON Files: Myths, Workarounds, and Alternatives

Lineserve TeamLineserve Team
·
4 min read

Ever tried to add a quick note or explanation to your JSON file, only to have it crash your application? You’re not alone. As developers, we often need to document our data structures for clarity, but JSON’s strict rules can make that tricky. In this guide, we’ll dive into the world of comments in JSON files—exploring the myths, practical workarounds, and smarter alternatives. By the end, you’ll understand why JSON stays comment-free and how to work around it effectively.

Understanding JSON’s Specification

JSON, or JavaScript Object Notation, is a lightweight data interchange format that’s easy for humans to read and write, and straightforward for machines to parse and generate. Defined by RFC 7159 and ECMAScript standards, JSON supports basic data types like objects, arrays, strings, numbers, booleans, and null.

Importantly, JSON’s design philosophy emphasizes simplicity and universality. It deliberately avoids features like comments to ensure it’s language-agnostic and focused on data transport rather than configuration files. This means standard JSON parsers will reject any file with comments, throwing syntax errors.

Myths and Realities of Comments in JSON

One common myth is that JSON supports comments because some tools or libraries seem to allow them. In reality, the official JSON specification does not include comments. Attempting to add them can lead to parsing failures.

For example, consider this invalid JSON with a comment:

{
  // This is a comment
  "name": "John",
  "age": 30
}

Standard parsers like JSON.parse() in JavaScript will throw an error: “Unexpected token /”. This strictness prevents ambiguity and keeps JSON lean for data exchange between systems.

Workarounds for Adding Comments

While official JSON doesn’t support comments, there are ways to simulate them during development. These are workarounds, not standards, so use them cautiously.

Using JSON5

JSON5 is a superset of JSON that adds features like comments, trailing commas, and unquoted keys. It’s great for configuration files where readability matters. Libraries like json5 can parse JSON5 syntax.

Example of JSON5 with comments:

{
  // User details
  name: "John",
  age: 30,
  /* Multi-line comment:
     This object represents a person */
  hobbies: ["reading", "coding"]
}

Use case: In a Node.js project, install json5 with npm install json5 and parse it like this:

const JSON5 = require('json5');
const data = JSON5.parse(fs.readFileSync('config.json5', 'utf-8'));

This is perfect for development configs where you need annotations.

Preprocessing Tools

You can use tools to strip comments before parsing. For instance, jsmin or custom scripts can remove comment lines.

Example workflow: Write a file like:

// Configuration file
{
  "server": "localhost",
  "port": 8080
}

Then use a tool to minify or preprocess it into valid JSON. In Python, the json module doesn’t support comments, but you can use json.loads after stripping with regex (though this is error-prone).

Tip: For safety, consider libraries like comment-json that handle comment stripping automatically.

Alternatives to JSON for Commented Configurations

If you need comments regularly, consider alternatives that are designed for human-readable configuration.

YAML

YAML (YAML Ain’t Markup Language) supports comments natively and is more flexible. It’s great for config files.

Example:

# User configuration
name: John
age: 30
# Hobbies list
hobbies:
  - reading
  - coding

Use case: Many tools like Docker Compose use YAML for configs with comments.

TOML

TOML (Tom’s Obvious, Minimal Language) is another option, popular in projects like Cargo for Rust.

Example:

# Configuration
[person]
name = "John"
age = 30

Both YAML and TOML can be parsed into objects similar to JSON, offering better readability.

External Documentation

For API responses or data interchange, keep JSON pure and store comments in README files or external docs. This maintains JSON’s integrity while providing explanations separately.

Best Practices and Tips

To avoid pitfalls, remember JSON’s core purpose: data interchange. Don’t force comments if they’re not needed for production.

  • Use comments sparingly: In development, JSON5 or alternatives are fine, but convert to standard JSON for deployment.
  • Validate your JSON: Tools like JSONLint ensure syntax correctness. For commented versions, test parsing thoroughly.
  • Common pitfall: Forgetting that non-standard JSON might not work across all environments—always check compatibility.
  • Best practice: Balance readability with performance. If comments bloat your file, consider external docs.

Pro tip: Use version control to track changes in commented files, and automate preprocessing in your build pipeline.

Conclusion and Next Steps

JSON’s lack of official comment support is intentional, keeping it simple for universal data exchange. Workarounds like JSON5 offer development flexibility, while alternatives like YAML provide better options for commented configs. By understanding these, you can choose the right approach for your project.

Next steps: Experiment with JSON5 in a small project, convert a config to YAML, and explore parsers that handle comments. If you’re stuck, check community forums for specific tool recommendations. Happy coding!

Share:
Lineserve Team

Written by Lineserve Team

Related Posts

Lineserve

AI autonomous coding Limitation Gaps

Let me show you what people in the industry are actually saying about the gaps. The research paints a fascinating and sometimes contradictory picture: The Major Gaps People Are Identifying 1. The Productivity Paradox This is the most striking finding: experienced developers actually took 19% longer to complete tasks when using AI tools, despite expecting […]

Stephen Ndegwa
·

How to Disable Email Sending in WordPress

WordPress sends emails for various events—user registrations, password resets, comment notifications, and more. While these emails are useful in production environments, there are scenarios where you might want to disable email sending entirely, such as during development, testing, or when migrating sites. This comprehensive guide covers multiple methods to disable WordPress email functionality, ranging from […]

Stephen Ndegwa
·

How to Convert Windows Server Evaluation to Standard or Datacenter (2019, 2022, 2025)

This guide explains the correct and Microsoft-supported way to convert Windows Server Evaluation editions to Standard or Datacenter for Windows Server 2019, 2022, and 2025. It is written for: No retail or MAK keys are required for the conversion step. 1. Why Evaluation Conversion Fails for Many Users Common mistakes: Important rule: Evaluation → Full […]

Stephen Ndegwa
·