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

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.



Solution 1:[1]

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
 }

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2