Trailing comma automatically inserted in JSON files

What happened?

I’ve been editing JSON files, and have found that when I save the file, it is automatically formatted (which is great!). However the issue is that the formatting adds a comma to the final entry, which is invalid. I.e. trying to load the JSON file in a program crashes.

E.g. it would turn:

{
  "key1": "val1",
  "key2": "val2"
}

into

{
  "key1": "val1",
  "key2": "val2",
}

when the file is saved.

What did you expect to happen?

Most of the formatting done by the system is correct, but this step is wrong, there should not be a trailing comma.

Steps to reproduce

  1. Open a JSON buffer
  2. Save it, observe changes

System information

My doom info is below, as well as the modes I have activated in JSON mode. I don’t think I’ve done any specific configuration of JSON mode, but I am wondering if anyone has this issue in stock Doom, or its an unintended side effect of some other configuration I have.


Loading data dump...

Loading data dump...

Can you try executing (format-all--probe)?

It should return the formatting tool being used (e.g. (prettier "json"))

Prettier has options for controlling the trailing commas… perhaps you have a .prettierrc file somewhere that is misconfiguring the formatting?

It also looks like you are opening JSON files in javascript mode instead of JSON mode? Can you enable :lang json in init.el and see if it will open files in json-mode after that?

This topic was automatically closed after 360 days. New replies are no longer allowed.