My IDE (WebStorm) lets me put comments in some of my JSON files (e.g., tsconfig.json
and .eslintrc.json
, but balks at allowing them in others (e.g., package.json
and app.json
).
The former are subsequently identified as "JSON with Comments" on GitHub (even if I don't actually have comments in them).
Why can I put comments in some JSON files but not others?
So while JSON is often closely associated with JavaScript (even though it's used by most other languages as well), it does not support the same commenting features as JavaScript as it's a data-only specification. Comments were removed from the JSON specification because they were being used as parsing directives, which ...
Can I add a comment in JSON? No, JSON is a data-only format. Comments in the form //, #, or /* */, which are used in popular programming languages, are not allowed in JSON. You can add comments to JSON as custom JSON elements that will hold your comments, but these elements will still be data.
If you're having trouble adding comments to your JSON file, there's a good reason: JSON doesn't support comments.
Open the JSON file in your text editor and add comments the same way you would in Python (using # and not docstrings) or the same way you would in JavaScript (using // and not multi-line comments using /** */ ).
By default the IDE checks for strict standard compliance, as standard JSON doesn't allow comments.
We have a set of options to relax compliance checking in Settings | Editor | Inspections, JSON and JSON5 | Compliance with JSON standard, and for some files the option for checking comments is off by default (tsconfig
, tslint
and eslint
configs), as corresponding tools allow comments in JSON (this is hardcoded).
If you need adding comments to certain JSON files, you can add their names to the list of patterns associated with JSON5 file type in Settings | Editor | File Types - JSON5 standard allows comments
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With