To make Visual Studio Code validate strict JSON instead of allowing comments, you must change the file association from "JSON with Comments" (jsonc) to standard "JSON" within your workspace or global settings. By default, VS Code often treats files with certain extensions or patterns as JSONC, which allows for single-line and multi-line comments, but this can lead to syntax errors when the file is eventually consumed by a strict parser in a production environment or a different programming language.

What You'll Need

  • Visual Studio Code installed on your computer (Windows, macOS, or Linux).
  • A JSON file that currently allows comments but needs to be strictly validated.

Step-by-Step

  1. Open Visual Studio Code and locate the file you wish to change in the Explorer sidebar or via the File menu.
  2. Look at the bottom right-hand corner of the VS Code window. You will see a status bar indicator that displays the current language mode for the active file. It likely says "JSON with Comments" or "JSONC".
  3. Click directly on the text "JSON with Comments" in that status bar. This action will open a dropdown menu at the top center of the editor window.
  4. In the dropdown menu, start typing "JSON" to filter the list of available languages.
  5. Select the option labeled simply "JSON" from the list. Note that this is distinct from "JSON with Comments". Once selected, the status bar at the bottom should now display only "JSON".
  6. To ensure this change is permanent for all files of this specific type or extension, you should modify your User Settings. Go to the top menu and select File > Preferences > Settings (on macOS, it is Code > Settings > Settings).
  7. In the Settings search bar at the top, type "files.associations".
  8. Locate the "Files: Associations" section. Click the "Add Item" button.
  9. In the "Item" field (the key), enter the file pattern you want to target, such as "*.json".
  10. In the "Value" field, enter "json". This tells VS Code that every time it encounters a file ending in .json, it must treat it as strict JSON, regardless of any previous heuristics.
  11. Save your settings by closing the Settings tab. Now, if you attempt to type a comment (using // or /* */) in a .json file, VS Code will immediately highlight it with a red squiggly line, indicating a syntax error.

Common Mistakes

One of the most frequent mistakes is confusing the file extension with the language mode. A file named config.json is technically a JSON file, but VS Code's internal engine might decide to treat it as JSONC if it detects certain patterns or if a specific extension (like a configuration extension for a specific tool) has overridden the default behavior. Users often think they have a broken installation when they see red lines, not realizing they have actually succeeded in enabling strict mode.

Another mistake is applying the change only to the current session. If you click the language mode in the status bar, it only changes the mode for that specific open tab. If you close the file and reopen it, or open a new file, VS Code will revert to its default detection logic. To make the validation strict every single time you open a file, you must use the "Files: Associations" method described in the step-by-step guide.

Finally, users sometimes try to fix JSON errors by adding comments to "explain" the code. In strict JSON, comments are illegal characters. If you are working in a strict environment, adding a comment to explain a complex nested object will cause the entire file to fail validation in your application, even if VS Code is no longer showing an error because you switched to strict mode.

Tips

If you are working on a large project with multiple sub-directories, you can use Workspace Settings instead of User Settings. This is highly recommended if you want strict JSON validation for one specific project (perhaps because it is being used by a strict API) but want to keep the flexibility of JSONC for your personal projects. To do this, create a folder named .vscode in your project root and add a settings.json file inside it. Any associations defined here will only apply to this specific project folder.

Use the "Format Document" command (Shift + Alt + F on Windows/Linux or Shift + Option + F on macOS) after switching to strict mode. This will help you see if your structural indentation is correct. If the formatter fails, it is a clear sign that you have a syntax error, such as a trailing comma, which is a very common violation in strict JSON that is often permitted in JSONC.

When working with complex data, keep a "Strict JSON Validator" website or a command-line tool like jq handy. While VS Code is excellent, having a secondary way to verify your syntax provides an extra layer of confidence before you commit your changes to a repository.

Troubleshooting

If the language mode does not change after you select "JSON" from the menu, try restarting the VS Code window. You can do this quickly by opening the Command Palette (Ctrl + Shift + P on Windows/Linux or Cmd + Shift + P on macOS) and typing "Developer: Reload Window". This refreshes the editor's internal state without requiring a full application restart.

If you have added an entry to "Files: Associations" but it seems to be ignored, check if you have any installed extensions that manage JSON files. Certain extensions for specific languages (like those for specialized configuration formats) might have higher priority in the association hierarchy. You can check this by looking at the "Extensions" view and disabling any JSON-related extensions temporarily to see if the behavior changes.

If you see red error lines even when there are no comments, you likely have a trailing comma. In JSONC, a comma after the last item in an array or object is often tolerated, but in strict JSON, it is a fatal error. Remove the extra comma and the error should disappear. Another common issue is the use of single quotes instead of double quotes; strict JSON requires double quotes for all keys and string values.

Frequently Asked Questions

Why does VS Code allow comments in my JSON files by default?

VS Code often defaults to JSONC (JSON with Comments) because many modern configuration files, such as those used by VS Code itself (settings.json) or various build tools, use comments to provide instructions or documentation to the user.

Will changing this setting break my existing files?

It will not break the files themselves, but it will change how VS Code displays them. If your files currently contain comments, they will now show syntax errors. You will need to remove the comments to make the files valid under strict JSON rules.

Can I have different rules for different folders?

Yes. By using Workspace Settings (the .vscode folder) instead of User Settings, you can define strict JSON validation for one project while allowing comments in others.

What is the difference between JSON and JSONC?

JSON (JavaScript Object Notation) is a strict data interchange format that does not support comments. JSONC is a superset of JSON that allows for single-line (//) and multi-line (/* */) comments, commonly used for configuration files.

How do I fix a trailing comma error?

Look at the end of your arrays [] or objects {}. If there is a comma after the very last element before the closing bracket, delete that comma. Strict JSON requires that the last element is not followed by a separator.

Does this affect how my code runs in Python or Node.js?

No, this only affects the visual validation and error highlighting within the VS Code editor. However, it is a good practice to match your editor settings to your runtime environment to avoid "it works in my editor but fails in production" scenarios.