How to Format JSON in VS Code: Options, Limits and What Actually Changes
Format JSON in VS Code with the built-in formatter, a formatter extension or format-on-save. Learn what formatting changes, what it leaves alone and when output may break.
Formatting JSON in Visual Studio Code makes data easier to read by adding consistent indentation and line breaks. It is not meant to change the data. This guide explains what a formatter does, compares the main ways to format JSON in the editor, and covers where formatted output can stop working once you copy it somewhere else.
No test results, version-specific measurements or benchmarks were supplied for this article. Descriptions of VS Code behavior refer to its documented general features. Menus, defaults and extension behavior can differ between versions and setups, so confirm details in your own editor.
What JSON formatting actually changes
A JSON formatter, also called a pretty-printer or beautifier, rearranges the whitespace between tokens. Tokens include braces, brackets, colons, commas, keys and values. The formatter adds line breaks after opening braces and commas, indents nested levels, and normalizes spacing around colons. Keys, values, string contents and the order of properties should stay the same.
Before:
{"name":"widget","tags":["a","b"],"size":{"w":10,"h":4}}
After (2-space indentation):
{
"name": "widget",
"tags": [
"a",
"b"
],
"size": {
"w": 10,
"h": 4
}
}
Both versions represent the same data. A program that parses either one gets identical keys and values, because whitespace outside of strings has no meaning in JSON.
There are two kinds of tools, and the difference matters:
• Whitespace-only formatters edit the spacing around tokens and leave the original text of each token alone.
• Parse-and-reserialize tools read the JSON into memory and write it out again. These can change more than spacing. They may rewrite escape sequences in strings, change how numbers are written, collapse duplicate keys or sort properties, depending on their options.
If exact text matters, for example in signed payloads, fixtures compared byte-for-byte, or files with intentional escapes, compare the result with a diff before you commit it.
How formatting differs from other text transformations
Pretty-printing keeps the meaning. Other text transformations do not:
• Character-order reversal produces invalid JSON.
• Unicode "styled" characters, such as mathematical bold letters, are different code points. A key written with them no longer matches the plain-ASCII key a program expects.
• CSS styling or bidirectional display controls only affect how text looks on screen. They do not change the underlying file.
If you need JSON that both people and programs can read, use whitespace formatting only.
Ways to format JSON in VS Code
Built-in Format Document command
VS Code includes JSON language support. With a .json file open, or the language mode set to JSON, follow these steps:
1) Open the Command Palette.
2) Run Format Document to format the whole file, or select some text and run Format Selection.
The documented default shortcuts for Format Document are:
• Windows: Shift+Alt+F
• macOS: Shift+Option+F
• Linux: Ctrl+Shift+I
Shortcuts can be remapped, so the Command Palette is the most reliable way to find the command.
If the file has no extension or opens as plain text, the JSON formatter will not apply. Set the language mode to JSON from the status bar first.
Format on save
The editor.formatOnSave setting formats a file every time you save it. You can enable it for JSON files only by placing it inside a language-specific block in settings.json:
"[json]": {
"editor.formatOnSave": true
}
This keeps formatting consistent without manual steps. The drawback is that saving any existing file reformats all of it. That can produce large diffs in shared repositories whose files were formatted with different rules.
Indentation settings
Indentation width and the choice between spaces and tabs come from the editor.tabSize and editor.insertSpaces settings. You can set both inside the same language-specific block. VS Code may also detect indentation from the file's existing content, so a file you open might keep its current style instead of using your defaults.
Formatter extensions
Teams that format several file types with a shared configuration often install a formatter extension. Prettier is a common choice and documents JSON support. When more than one formatter is available for JSON, set editor.defaultFormatter inside the [json] block. This makes VS Code use the same formatter every time. Settings, options and output are defined by each extension, not by VS Code, so check that extension's documentation for exact behavior.
Comparing the options
| Option | Best for | Watch out for |
| Built-in Format Document | Quick, one-off cleanup of a file or selection | Requires the JSON language mode; uses editor indentation settings |
| Format on save | Keeping your own files consistently formatted | Reformats the whole file on save, which can create noisy diffs |
| Formatter extension with editor.defaultFormatter | Shared team style across many file types | Behavior depends on the extension and its config file |
| Command-line or online tools outside VS Code | Scripts, pipelines or machines without the editor | Parse-and-reserialize tools may change more than whitespace; online tools receive your data |
Validation: formatting is not the same as checking
A formatter needs syntactically valid input. Common errors, such as a missing comma, an unclosed bracket or single-quoted strings, usually stop formatting or produce unexpected results. VS Code's JSON support reports syntax errors in the editor and in the Problems panel. Fix those errors first, then format.
Valid syntax does not mean the data has the right structure. VS Code can also check JSON against a JSON Schema, set through the json.schemas setting or a $schema property in the file. A schema can detect a missing required field or a wrong value type. A formatter cannot.
JSON with Comments is a different mode
Standard JSON does not allow comments. VS Code has a separate JSON with Comments mode (jsonc), which it uses for its own settings files and which tolerates comments. Formatting a jsonc file does not make it standard JSON. If a file must be read by a strict JSON parser, remove the comments and any trailing commas, and confirm the language mode is JSON so the editor flags them.
Will formatted JSON stay readable and usable elsewhere?
Formatted JSON is plain text. It contains ordinary spaces or tabs and line breaks, with no special characters. When you paste it into another code editor, a terminal or a plain-text field, the formatting generally comes through intact. What happens next depends on the destination, not on VS Code.
Some destinations can damage JSON when you paste it:
• Word processors, rich-text email and some chat or document tools may change straight quotes to curly "smart" quotes. The result is no longer valid JSON.
• Some fields collapse repeated spaces or strip line breaks. The data stays valid, but the readable layout is lost.
• Rich-text paste may carry editor colors as styling. The styling does not affect the text, but it can look inconsistent.
Where the destination supports it, paste into a code block or plain-text field. To confirm the data survived, copy it back out and paste it into VS Code. Any errors will show up there.
Readability has limits too. Deeply nested structures stay hard to follow even with indentation. Very long single-line strings still wrap or scroll. Code folding in VS Code can collapse nested sections. No tested evidence was supplied on how screen readers handle indented JSON, so this article makes no claim about that. If accessibility matters for your audience, test with the assistive technology they use.
Large files have their own limits. VS Code may turn off some language features for very large files to stay responsive, which can affect formatting or validation. No size threshold was supplied here. If formatting fails or is unavailable on a large file, a command-line tool is a practical fallback, keeping in mind the reserialization caveats above.
Handling sensitive JSON
JSON often contains API keys, tokens, personal data or internal configuration. Formatting a file in your local editor does not require sending its contents anywhere. Online JSON formatters do receive whatever you paste, and you usually cannot verify how they store or log it. Extensions can also behave differently from one another, so review an extension's documentation and permissions before using it on confidential files.
For sensitive data, use the built-in formatter or a trusted local tool. If you must use an external service, replace secrets and personal values with placeholders first.
Choosing by situation
- Cleaning up a single file or pasted snippet: run Format Document from the Command Palette.
- Keeping your own JSON files tidy over time: enable format-on-save for the [json] language.
- Matching a team's shared style: use the formatter the project already uses and set it as editor.defaultFormatter for JSON.
- Exact text that must not change: format, then review the diff to confirm only whitespace moved.
- Data headed for a strict parser: check that the file is in JSON mode rather than JSON with Comments, and fix every reported problem.
- Confidential content: stay with local tools and avoid pasting it into online formatters.