In VS Code I don't want Black to format my settings.json

Viewed 2065

I want to use the Black formatter for my Python files, but not for my JSON settings.

I have these set in my settings.json:

    "python.formatting.provider": "black",
    "editor.formatOnSave": true,

I have tried to use the --exclude tag by adding the following to settings.json:

    "python.formatting.blackArgs": [
        "--exclude /*\\.json/"
    ],

which is equivalent to a commandline call with black --exclude /*\.json/

I also tried

    "python.formatting.blackArgs": [
        "--exclude /*\\.json/"
    ],

based on this post: VS Code Python + Black formatter arguments - python.formatting.blackArgs.

However, it is still formatting my settings.json.

3 Answers

Black doesn't format JSON. What's happening is VS Code has it's own included JSON formatter and that's what is formatting your settings.json. Do you have a setting turned on like "editor.formatOnSave" turned on? If so then it sounds like you want to scope it to just Python files, e.g.:

"[python]": {
  "editor.formatOnSave": true
}

You can also disable the formatting for JSON, by:

  • going to the preferences with [ctrl+,] and disabling JSON > Format: Enable.
  • opening the settings.json file (open palette with [ctrl+shift+p] and search for "settings json") and add the following line:
    "json.format.enable": false
    

Or you can also limit the formatting to python files, by adding this setting in the settings.json file:

"[python]": {
   "editor.formatOnSave": true
 }

Black does format JSON and for me, broke it:

╰─➤  black proj/
reformatted proj/schema.json
All done! ✨  ✨
1 file reformatted.

╰─➤  git diff proj/schema.json | wc -l
299
Related