There are number of extensions in Visual Studio Code (VS Code) to prettify JSON files. But is there an extension or setting, which enables me to view all *.json files pre-formatted / pre prettified? I find it tedious to constantly press shift-alt-F + ctrl-s when viewing ugly one line json-data, especially if the content of the file is being updated regularly back to pre-formatted form.
5 Answers 5
On Windows: Shift + Alt + F
On Mac: Shift + Option + F
On Linux: Ctrl + Shift + I
Original answer How do you format code in Visual Studio Code (VSCode)?
I'm adding it here because when I was looking for it I found your post before the other one. Hope it helps.
3 Comments
category:formatters plaintext prettier. So, I installed Prettier. Next time I've pressed the combo keys I was required to setup the default formatter. And that's it.you can dowlnoad JSON from Meezilla extension and then Press Cmd/Ctrl+Shift+J to quick beautify with default config.
1 Comment
Be aware that if you have the setting
JSON > format: Keep Lines
enabled (the default is disabled), that a one-liner JSON file (or any JSON that has multiple key/values on the same line) will not be formatted as you wish. The file might not change at all until you disable the Keep Lines setting and re-format.
1 Comment
"json.format.keepLines": falseYou can download JsonPrettifier extension.
1 Comment
Since none of the answers really give an answer to the original question, is there a way to view automatically prettified JSON in Visual Studio Code, I'll here provide a workaround that may help if one has tons of JSON to view that are not prettified. This workaround presents a method to Prettify all of them at once.
VSCODE Prettify multiple JSON files trick, no extensions or plugins needed
(tested on MacOS, VSCODE Version: 1.105.1)
Open Visual Studio Code (VSCODE)
Select the Preferences: Open User Settings (JSON) command in the Command Palette using shortcut - ⇧⌘P
Check that under JSON section there is setting - "editor.formatOnSave": true,
"[json]": { "editor.defaultFormatter": null, "editor.formatOnPaste": false, "editor.editContext": false, "editor.formatOnType": false, "editor.wordWrap": "on", "editor.formatOnSave": true, "files.autoSave": "off" },Modify the - settings.json - file if necessary, save and close it.
Drag the folder that has multiple not prettified JSON files into middle of VSCODE window.
NOTE! It's recommended to take a backup copy of this folder in case you make a mistake in following these instructions.If file Explorer doesn't appear automatically for some reason, open it from the top left symbol that has two papers on top of each other
Click on the first JSON file on the list and its contents should appear on the right panel
Use keyboard shortcut - ⌘A - to select all the files on the list
Close the just opened file on the right panel ( X symbol after the name tab), if there is any files with contents visible, those files will not be prettified.
Click the magnifying glass symbol on left side of the window
Type on both fields just plain comma - , - (without these lines or other characters), since commas are found in every JSON document
Click - .* - symbol at the end of the first line where you placed the comma, multiple search results should appear below these fields
Click then a symbol below that - .* - symbol, it should show a hover text - Replace All - on top of it when you move your cursor on top of it
Answer to the question replacing those commas with similar commas - Replace - after which there should be a message at the upper left corner saying something like - Replaced XXX occurences across XX files with ','.
Click the Explorer symbol above the magnifying glass and select any file from the list to see that it should be now prettified. VSCODE has saved them all, too (even though - files.autoSave - variable above in the settings has been turned off.
Comments
Explore related questions
See similar questions with these tags.