Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I put comments in some JSON files but not others?

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?

like image 487
orome Avatar asked Aug 05 '19 18:08

orome


People also ask

Why JSON not allow comments?

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 ...

How do I add a comment to a JSON file?

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.

Can we comment a line in JSON file?

If you're having trouble adding comments to your JSON file, there's a good reason: JSON doesn't support comments.

How do you comment a block of code in JSON?

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 /** */ ).


1 Answers

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

like image 163
lena Avatar answered Nov 15 '22 11:11

lena