Search code examples
jsonvisual-studio-codecomments

What happens when you select JSON with Comments as the Language Mode on VSCode?


When the Language Mode is set to JSON (auto-detects based on file extension), I get the error:

enter image description here

enter image description here

But if I set it to JSON with Comments, the error is gone:

enter image description here

enter image description here

What I want to know is what is happening exactly when I set it to JSON With Comments:

It's pretty obvious that VSCode stops complaining about comments in JSON files.

But are the comments removed when some package needs to read those files? I usually need to comment inside configuration files like, for example:

  • .eslintrc.json
  • firebase.json

Am I risking "corrupting" a configuration file by commenting on it? Is it something safe to do? In my examples, would eslint and firebase ignore the comments? How does it work?


Solution

  • Whether or not comments are supported in a given JSON file is entirely up to how the file will be used. .eslintrc.json files for example can have comments because the eslint library/tool can parse them properly, however many other tools will complain if they encounter comments in a json

    VS Code's two json language modes (JSON vs JSON with Comments) only determine how VS Code and its extensions interpret the file in the editor. They do not change the file's content or effect how other applications interpret the file. (i.e. changing modes in VS Code should not "corrupt" the file)

    • Use the JSON language mode if you are writing json that needs to be standard json. VS Code will help enforce this by warning if you add a comment or trailing comma

    • Use the JSON with Comments language mode if you are writing json for an application that supports comments (like .eslintrc.json)