void.pointer
void.pointer

Reputation: 26315

How do I control the formatting style of JSON in Visual Studio Code?

There is apparently built in support in VS Code to format JSON files. However, I do not see a way to customize the way the formatting is done (e.g. specifying line breaks before curly-braces, brackets, etc). I have also read some documentation related to this but it doesn't go into any detail. Is there a way to customize how the formatting works in Visual Studio Code for JSON files?

Upvotes: 5

Views: 9433

Answers (2)

Mark
Mark

Reputation: 180641

There is one more json formatting option in Insiders v1.70 now.

JSON > Format: Keep Lines
Keep all existing new lines when formatting.

That is disabled by default. Enabling it will keep any empty lines in json documents when formatting AND keep multiple entries on the same line as in the original - see https://stackoverflow.com/a/73180702/836330 for an example.

{
    "big_integer_array": [12,15,13,1,5,8,15,14,12],
    "this_value_is_in_line_three": true
}

will stay that way and not drop down like

{
    "big_integer_array": [
        12,
        15,
        13,
        1,
        5,
        8,
        15,
        14,
        ...
      ]
}

like it will with the setting disabled. Also multiple key/value pairs can appear on the same line now.

Upvotes: 3

Toris
Toris

Reputation: 2376

Default formatter of JSON have only few features, like changing tab size.

Open Preferences with File > Preferences > Settings (Ctrl + ,) > User Settings (or Workspace Settings) tab > [JSON] "Edit in settings.json" link

or

Command Palette (Ctrl+Shift+P) > Preferences: Configure Language Specific Settings > JSON

and add language specific settings.

Refs: Creating User and Workspace Settings Language specific editor settings

settings.json (sample)

{
    "[json]": {
        // JSON specific settings goes here
        // "editor.tabSize": 4
    }
}

For further customization, use extensions found in market place.

Upvotes: 1

Related Questions