Visual Studio Code (VS Code) is a powerful and versatile code editor favored by developers worldwide for its extensive features and customization options. However, sometimes VS Code can throw errors that seem perplexing at first glance. One common issue users encounter is the “Comments are not permitted in JSON” error. This error appears because JSON, or JavaScript Object Notation, a lightweight data-interchange format, doesn’t natively support comments. While adding comments can be helpful for readability during development, they violate the JSON specification. This blog post will explore various methods to disable error “Comments are not permitted in JSON” in VS Code, ensuring a smoother and more efficient coding experience. We’ll cover extensions, settings configurations, and alternative strategies to effectively manage your JSON files within the VS Code environment.
Understanding the “Comments are not permitted in JSON” Error
The “Comments are not permitted in JSON” error arises when VS Code’s built-in JSON validator detects comments (lines starting with // or enclosed in / /) within a JSON file. JSON is designed as a data serialization format, not a configuration format meant for human editing. According to the official JSON specification [ json.org ], comments are explicitly disallowed. This is because different JSON parsers may handle comments inconsistently, leading to unpredictable behavior in applications that rely on these files. When VS Code flags this error, it’s essentially enforcing the JSON standard to prevent potential issues. “JSON’s minimalistic design ensures interoperability across different systems,” explains Douglas Crockford, the architect of JSON [ Crockford.com ].
The error can be particularly frustrating when you’re working with configuration files like tsconfig.json or .vscode/settings.json, where you might want to leave notes for yourself or other developers. The default VS Code behavior is to strictly adhere to the JSON standard, thus triggering the error whenever comments are detected. This behavior helps maintain the integrity of data exchange across various systems and prevents unexpected parsing issues. The need to remove all comments before using a JSON file can become cumbersome, especially when iteratively refining your configurations. This is where understanding how to manage this error within VS Code becomes essential for improving developer productivity and workflow.
Consider a scenario where a team is collaboratively working on a large project that utilizes multiple JSON configuration files. One developer adds extensive comments to explain the purpose of different settings, aiding in maintainability. However, another developer, unaware of the comments, attempts to parse the JSON file using a strict JSON parser, leading to a parsing error and potentially disrupting the application’s functionality. This situation highlights the importance of adhering to the JSON standard and finding alternative ways to document your configurations without directly embedding comments within the JSON file itself.
Methods to Disable or Circumvent the Error
While completely disabling the JSON validator might not be ideal, there are several ways to manage or circumvent the “Comments are not permitted in JSON” error in VS Code. These methods range from using extensions to adjusting VS Code settings or adopting alternative file formats that support comments. Each approach has its own trade-offs, and the best solution depends on your specific workflow and needs.
- Using Extensions: Several VS Code extensions can help you manage JSON files with comments more effectively.
- Adjusting VS Code Settings: VS Code offers settings that allow you to customize its behavior, including how it handles JSON validation.
- Alternative File Formats: Consider using formats like JSONC or YAML, which support comments natively.
Using VS Code Extensions
One of the easiest ways to handle comments in JSON files is to use a VS Code extension. Several extensions are available that either allow comments in JSON or provide alternative ways to document your configuration. The “JSONC” extension, for example, allows you to treat files with a .jsonc extension as JSON with Comments. This means you can freely add comments without triggering errors. Another useful extension is “Prettier - Code formatter” [ VS Code Marketplace ], which can be configured to automatically remove comments from JSON files before saving, ensuring they remain valid JSON. These extensions provide a convenient way to balance the need for comments during development with the requirement for valid JSON in production.
To use an extension, simply search for it in the VS Code Marketplace and install it. After installation, you may need to configure the extension’s settings to suit your preferences. For example, with the “JSONC” extension, you would rename your JSON file to have a .jsonc extension. With “Prettier,” you might need to adjust its settings to automatically strip comments on save. These extensions can significantly streamline your workflow by allowing you to work with comments without constantly worrying about validation errors. This approach maintains the readability and maintainability of your configuration files, especially in complex projects.
Consider a scenario where you are working on a complex tsconfig.json file with numerous compiler options. Using the “JSONC” extension, you can add detailed comments explaining the purpose of each option, making it easier for other developers (or your future self) to understand the configuration. Before deploying your project, you can use Prettier to automatically remove these comments, ensuring that the final tsconfig.json file is a valid JSON file that can be parsed by the TypeScript compiler. This hybrid approach combines the benefits of having comments during development with the necessity of having valid JSON in production.
Modifying VS Code Settings to Ignore the Error
While not generally recommended, you can modify VS Code settings to ignore the “Comments are not permitted in JSON” error. This approach should be used with caution, as it essentially disables the JSON validator and can lead to unexpected issues if your JSON files are not valid. However, in some specific cases, it might be a viable option. To modify the settings, open VS Code’s settings (File > Preferences > Settings, or press Ctrl+, on Windows/Linux or Cmd+, on macOS). Then, search for “json.validate.enable” and uncheck the box. This will disable JSON validation globally. Alternatively, you can add the following to your settings.json file: “json.validate.enable”: false.
It is crucial to understand the implications of disabling JSON validation. By doing so, you are essentially telling VS Code to ignore any syntax errors in your JSON files, including the presence of comments. This can be problematic if you accidentally introduce other errors in your JSON, as VS Code will no longer flag them. Therefore, this approach should only be used if you are absolutely certain that your JSON files are valid and that the only issue is the presence of comments. Furthermore, it’s a good practice to periodically validate your JSON files using an external validator to ensure their correctness.
For instance, if you are working on a project where you need to quickly iterate on a JSON configuration file and adding comments is essential for your workflow, disabling JSON validation might seem like a convenient option. However, you should always remember to re-enable validation before committing your changes or deploying your project. A better alternative would be to use one of the extensions mentioned earlier, such as “JSONC,” which allows you to have comments without disabling validation altogether. Disabling validation should be considered a temporary workaround rather than a permanent solution.
Using Alternative File Formats
Another effective strategy is to use alternative file formats that natively support comments. Two popular options are JSONC (JSON with Comments) and YAML (YAML Ain’t Markup Language). JSONC is essentially JSON with the addition of comments, and as mentioned earlier, extensions like “JSONC” in VS Code can handle these files seamlessly. YAML is a more human-readable data serialization format that explicitly supports comments and offers a more flexible syntax compared to JSON. Switching to YAML can significantly improve the readability and maintainability of your configuration files, especially in complex projects. Many tools and frameworks support YAML configuration, making it a viable alternative to JSON.
Migrating from JSON to YAML involves converting your existing JSON files to YAML format. This can be done manually or using online converters. Once you have converted your files, you will need to update your application or scripts to read and parse YAML files instead of JSON files. The process is generally straightforward, and many libraries are available for parsing YAML in various programming languages. While YAML offers several advantages over JSON, such as better readability and support for comments, it also has its own quirks and complexities. Therefore, it’s essential to understand the YAML syntax and best practices before making the switch.
Consider a scenario where you are managing a complex Kubernetes deployment configuration. Kubernetes supports both JSON and YAML for its configuration files. Switching from JSON to YAML would allow you to add comments to your configuration files, explaining the purpose of each resource and its settings. This would significantly improve the maintainability of your deployment configuration, especially when working in a team. Furthermore, YAML’s more human-readable syntax would make it easier to understand and modify the configuration files, reducing the likelihood of errors. This strategic shift enhances collaborative development and reduces potential deployment errors.
Here’s an example of the difference between JSON and YAML for a simple configuration:
JSON:
{ "name": "My Application", "version": "1.0.0", "description": "A simple application" }
YAML:
name: My Application version: 1.0.0 description: A simple application This is a comment
Step-by-Step Guide to Disabling the Error Using Settings
This section provides a step-by-step guide on how to disable error “Comments are not permitted in JSON” in VS Code by modifying the settings. This method is not generally recommended for long-term use, but it can be helpful in specific situations where you need to quickly work with JSON files containing comments.
- Open VS Code Settings: Open the settings by going to File > Preferences > Settings or by pressing Ctrl+, (Windows/Linux) or Cmd+, (macOS).
- Search for JSON Validation: In the settings search bar, type “json.validate.enable”.
- Disable JSON Validation: Uncheck the box next to “Json โบ Validate: Enable” to disable JSON validation globally. Alternatively, click on “Edit in settings.json” to modify the settings.json file directly.
- Modify settings.json (Optional): If you chose to edit the settings.json file, add the following line: “json.validate.enable”: false. Save the file.
- Restart VS Code (Optional): In some cases, you may need to restart VS Code for the changes to take effect.
Featured Snippet Optimized: The quickest way to disable error “Comments are not permitted in JSON” in VS Code is to uncheck the “Json โบ Validate: Enable” setting in the VS Code settings. Access settings via Ctrl+, (or Cmd+, on macOS), search for “json.validate.enable”, and uncheck the box. This will globally disable JSON validation, allowing comments without errors. Be aware that this also disables other validation checks, so use with caution.
Remember that disabling JSON validation globally can have unintended consequences. It’s generally better to use extensions or alternative file formats that support comments natively. However, if you need to quickly disable the error for a specific file, this method can be a temporary solution. Always remember to re-enable validation when you are finished working with the file to ensure that your JSON files are valid.
- Why does JSON not allow comments?
- JSON is designed as a data serialization format for data interchange, not a configuration format for human editing. Comments can lead to inconsistencies in parsing across different systems. [RFC 8259](https://www.rfc-editor.org/rfc/rfc8259) explains these limitations.
- Is it safe to disable JSON validation in VS Code?
- Disabling JSON validation should be done with caution, as it can hide other syntax errors in your JSON files. It's generally better to use extensions or alternative file formats.
- What are the alternatives to using comments in JSON?
- Alternatives include using JSONC (JSON with Comments), YAML, or keeping comments in separate documentation files.
- How do I use the JSONC extension in VS Code?
- Install the JSONC extension from the VS Code Marketplace and rename your JSON files to have a .jsonc extension. You can then add comments without triggering errors.
- Can Prettier remove comments from JSON files?
- Yes, Prettier can be configured to automatically remove comments from JSON files before saving, ensuring they remain valid JSON.
I sometimes use Visual Studio Code to edit JSON files that include comments. VS Code displays an error saying, “Comments are not permitted in JSON.” It would be nice to disable that error message (without having to remove the comments.)
Follow these steps:
- Click on the letters JSON in the bottom right corner. (A drop-down will appear to “Select the Language Mode.”)
- Select “Configure File Association for ‘.json’…”
- Type
jsoncand press Enter.
If you only want to disable the error message for a single file, skip step #2.


