vscode: disable spaces addition inside brackets in JSON files

106 Views Asked by At

I'm editing a JSON file in VSCode. Everytime I save the document, spaces are added to the begining and end of the brackets. For ex, if I've the following code.

{
  "one": ['1', '11']
}

It get's converted to the following when the document is saved.

{
  "one": [ '1', '11' ]
}

Spaces at the first and end of the brackets are automatically added. How can I disable this?


VsCode Version:

code --version
1.85.1
0ee08df0cf4527e40edc9aa28f4b5bd38bbff2b2
x64

Extensions: I've deleted the ~/.vscode and reinstalled the app, so it's pretty brand new.

  • Tried Reinstalling
  • Tried removing all the related extensions
1

There are 1 best solutions below

2
Lê Hoàng Chinh On

It seems like the behavior you're experiencing might be related to the VSCode formatting settings. To adjust this, you can modify the settings to match your preferred JSON formatting style. Here are steps to configure JSON formatting in VSCode:

Open VSCode settings:

  • On Windows/Linux: Press Ctrl + , or navigate to File > Preferences > Settings.
  • On macOS: Press Cmd + , or navigate to Code > Preferences > Settings. Click on the "Open Settings (JSON)" icon located at the top-right corner of the settings window. This will open the settings.json file.

Add or modify the following settings in settings.json:

json

// Controls whether the editor should automatically format the file on save.
"editor.formatOnSave": true,

// Enable/disable default JavaScript formatter (e.g., for JSON files).
"[javascript]": {
  "editor.defaultFormatter": "vscode.json-language-features"
},

// Enable/disable default TypeScript formatter (e.g., for JSON files).
"[typescript]": {
  "editor.defaultFormatter": "vscode.json-language-features"
},

// Configure the format settings for JSON files.
"editor.formatOptions": {
  "insertSpaces": true,
  "tabSize": 2,
  "singleQuote": false,
  "trailingComma": "none",
  "bracketSpacing": true
}

Adjust the values in the editor.formatOptions section according to your preferences. For example, set "bracketSpacing": false to disable spacing around brackets.

Save the settings.json file.

After making these changes, VSCode should format your JSON files according to the specified settings on save. If the issue persists, you may want to check for any extensions that might be affecting the behavior and temporarily disable them to see if the problem is resolved.