4

Usually i am creating valid JSON objects like this:

{
  hasPermission: true,
  notificationStatusId: 1
};

ON VSCode, when is save the file, sometimes it's adding a tailing comma automatically after the last property like this:

{
  hasPermission: true,
  notificationStatusId: 1,
};

Actually I'm not asking how to disable that behavior and i know how to do that. i am asking what is the reason VSCode has that feature of adding a trailing comma automatically?

3
  • 1
    That's not JSON, where a trailing comma would be a problem, it's JS. Presumably something (Prettier or some other linter) is configured to format on save.
    – jonrsharpe
    Nov 20, 2019 at 12:33
  • It's valid JSON5 Nov 21, 2019 at 4:53
  • Oh but there's a that trailing semicolon too... which isn't valid json anything (and this is also not even valid js) Nov 21, 2019 at 5:02

3 Answers 3

5

First, make sure the file's language mode is set to just JSON (not JSON with comments or JSON5 or JSON takes Manhattan). The language mode is displayed in the lower right corner of the status bar:

JSON language mode

VS Code will warn you about trailing commands in the JSON language mode.

As for why the trailing comma is being inserted, that is likely caused by one of your extensions as VS Code should not do this by default. Try going through your extensions to identify which one is causing this

4

If you are using some linter like prettier, Recently Default value of Trailing commas changed from "none" to "es5" in new versions To fix this you have to change configuration of your linter In case of prettier, create a file .prettierrc.json with content

{"trailingComma": "none"}
1

The trailing comma is probably added by prettier code formatter. This is recommended for javascript, and disallowed for JSON according to the documentation:

Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can add a new line without modifying the previously last line if that line already uses a trailing comma. This makes version-control diffs cleaner and editing code might be less troublesome.

JavaScript has allowed trailing commas in array literals since the beginning, and later added them to object literals (ECMAScript 5) and most recently (ECMAScript 2017) to function parameters.

JSON, however, disallows trailing commas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.