22

In Visual Studio Code (under Windows), my present Json files are displayed as the following :

enter image description here

I would them to be formatted as displayed below :

enter image description here

2
  • 9
    Ctrl-Shift-P -> format document? Commented Jan 16, 2022 at 8:15
  • 1
    Note) 'Format Document' (Shift+Alt+F in Windows) in vscode "actually" modifies the content of the file. I mean, this is not just showing indented content for readability without modifying the actual content of the json file. Commented Apr 22, 2024 at 12:52

10 Answers 10

30

Use ALT-SHIFT-F to auto format a json file in windows

Sign up to request clarification or add additional context in comments.

Comments

25

Open the json file and SHIFT + OPTION + F for mac

Comments

8

In 2022 it's Ctrl+Shift+I as stated on the Visual Studio Code website.

1 Comment

Works in VSCodium as well.
5

RIGHT CLICK and Select Source Action then JSON file will be converted to JSON format

enter image description here

Comments

2

Press Ctrl-Shift-p to format the documents. I don't know if this shortcut works on Windows, but it does on Linux.

1 Comment

No, it doesn't. I was trying to process a step in a tutorial where the author works on Mac. I will also try later to find an extension for the right format.
2

If you need to setup the formatting to be consistent on every project you work, you can create a .vscode directory on the project root, create a settings.json file inside the .vscode directory.

To format JSON files you can add the following json to the settings.json file.

{
  "[json]": {
    "editor.defaultFormatter": "vscode.json-language-features",
    "editor.tabSize": 2,
    "editor.formatOnSave": true,
    "editor.insertSpaces": true,
    "editor.detectIndentation": false,
  }
}

This would format the file when you save it. You can set tabSize to whatever size you prefer. insertSpace would add spaces instead of tabs when you use tabs. detectIndentation would ignore the current file indentation method and will stick to your specific settings.

Comments

1

On your Mac, you can do so by pressing command+shift+p and then type or select "JSON: Sort Document"

Comments

0

Install a code formatting extension then save the file.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Install Prettier using the extensions, and then right click, select format document with and pick Prettier.

Comments

0

For MacOS, if shortcuts didn't worked there might be conflicts between the formatters plugins. Try

select the code -> right click -> choose format selection

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.