Formatting JSON in VS Code
How to format a JSON file in Visual Studio Code, what formatting actually changes, when it is safe to use, and when compact or byte-exact JSON is the better choice.
Visual Studio Code can reformat JSON with its built-in tools, with no extension required. This review covers how the formatter works, what it changes in your data and what it leaves alone, and when you should keep the original or compact form.
Short verdict
For reading, reviewing and hand-editing JSON, the built-in formatter is a good fit. Its main benefit is that it rewrites only the whitespace between tokens, so the data a JSON parser produces stays the same while the structure becomes much easier to scan. The decisive tradeoff is that the file's bytes change. Keep the original or a minified version when exact bytes matter, when size matters, or when a destination expects the JSON on a single line.
How to format a JSON file in VS Code
- Open the file. Check that the language mode in the status bar reads JSON, or JSON with Comments.
- Run Format Document. You can find it in the Command Palette (Ctrl+Shift+P on Windows and Linux, Cmd+Shift+P on macOS).
- The default shortcut is Shift+Alt+F on Windows, Shift+Option+F on macOS and Ctrl+Shift+I on Linux. These bindings can be remapped, so yours may differ.
- To format only part of a file, select the text and run Format Selection.
- If you pasted JSON into an untitled tab, set the language first with Change Language Mode. The JSON formatter is not offered for plain text.
Settings that control the result
- editor.tabSize and editor.insertSpaces set the indentation width and whether tabs or spaces are used. You can scope them to JSON with a language-specific block in settings.json.
- editor.formatOnSave formats the file automatically whenever you save it.
- editor.defaultFormatter chooses which formatter handles JSON when more than one is installed, such as the built-in one and a third-party extension like Prettier.
- json.format.enable turns the built-in JSON formatter on or off.
- files.eol and files.insertFinalNewline control line endings and the trailing newline. These also change the file's bytes.
What formatting actually changes
Formatting is a whitespace transformation. JSON treats spaces, tabs and line breaks between tokens as insignificant, so the formatter can add indentation and line breaks without changing the values. The formatter does not:
• reverse text
• substitute characters
• apply visual styling
• change the order of characters inside strings
Before:
{"name":"Ada","tags":["math","code"],"active":true}
After (two-space indentation):
{
"name": "Ada",
"tags": [
"math",
"code"
],
"active": true
}
In this example, every key, value and character inside every string is identical before and after. Only whitespace outside the strings was added. The exact layout depends on your settings and the formatter you use. For example, some formatters keep short arrays on one line.
| Aspect | Changed by formatting? | Notes |
| Indentation and line breaks | Yes | This is the purpose of the operation |
| Keys, values and string contents | No | Whitespace inside strings is part of the value and is kept |
| Escape sequences such as \u00e9 or \n | No | They stay as written and are not decoded or re-encoded |
| Key order | No | The built-in formatter does not sort keys |
| Invalid syntax | Not repaired | Errors appear in the Problems panel |
| Comments | Kept in JSON with Comments mode | Comments are flagged as errors in strict JSON mode |
| File bytes, size and hash | Yes | Any byte-sensitive check will see a different file |
"Format" here means pretty-printing, the opposite of minifying. It has nothing to do with fonts or styled Unicode text.
Will formatted JSON still work after copying or sharing?
Formatted JSON is still valid JSON. Any conforming parser should read it the same way as the compact version, so copying it into another file, a configuration system or an API client is generally safe.
The risks come from the destination, not from VS Code. Two common examples:
• A rich-text editor or messaging app may alter quotes or collapse whitespace.
• A field that accepts a single line may reject or truncate multi-line input.
This review has no tested evidence for any specific destination. Check the result by pasting it back into VS Code and confirming the Problems panel shows no errors.
For accessibility, indentation mainly helps sighted readers follow nesting. This review has no documentation or test results on how individual screen readers announce indentation in VS Code or elsewhere. If the JSON will be read with assistive technology, test it with that tool rather than assuming indentation carries the structure.
When compact or unformatted JSON is preferable
- When a signature, checksum or cache key is computed over the exact bytes of the file.
- When payload size or storage matters and nobody reads the file directly.
- When the destination requires a single line, such as some environment variables, log formats or one-record-per-line files.
- In version control, when reformatting an existing file would bury the real change in whitespace-only lines. Consider formatting in a separate commit.
- When the file is not valid JSON yet. Fix the syntax first, because the formatter will not do it for you.
Gaps and limitations
This review is based on the general documented behavior of VS Code and the JSON format. It has no measurements of formatting speed on large files and no version-specific test results. It also has no tests of how particular extensions, chat tools or screen readers handle formatted output. If your workflow depends on any of these, run a small test on your own setup before relying on it.