Pretty Print JSON in VS Code: What Formatting Changes and When to Use It
How to pretty print JSON in Visual Studio Code, what the formatter actually changes, how it differs from validation, and when compact plain JSON is the better choice.
Pretty printing JSON in Visual Studio Code is a whitespace transformation. It adds line breaks and indentation so nested objects and arrays are easier to scan. It does not change keys, values or structure. This review covers how to do it, what it changes, and when to leave JSON compact.
Short verdict
For reading, reviewing and hand-editing JSON, the built-in formatter is a good fit. It needs no extension and does not alter the data, because the JSON specification treats whitespace between tokens as insignificant. The main tradeoff is that formatting changes the file's exact bytes and line layout. Keep the original compact or byte-identical form when size, exact hashes or signatures, or minimal version-control diffs matter more than readability.
How to format JSON in VS Code
VS Code includes JSON language support, so formatting works with no extra setup once the editor recognizes the file as JSON. Files ending in .json are detected automatically. For an untitled buffer or pasted text, set the language mode first. Click the language name in the status bar, or run Change Language Mode from the Command Palette, and choose JSON. Choose JSON with Comments if the content contains comments.
- Format the whole file: run Format Document from the Command Palette. The default shortcuts are Shift+Alt+F on Windows, Shift+Option+F on macOS and Ctrl+Shift+I on Linux.
- Format only part of a file: select the text and run Format Selection (default Ctrl+K Ctrl+F on Windows and Linux, Cmd+K Cmd+F on macOS).
- If several formatters are installed, for example the built-in one and Prettier, VS Code may ask which to use. Run Format Document With to pick one and optionally set it as the default.
- Shortcuts can be remapped, so check the Keyboard Shortcuts editor if the defaults do nothing.
To format automatically on save for JSON only, scope the setting to the language in your settings.json:
"[json]": {
"editor.formatOnSave": true
}
Indentation width comes from the editor's tab size and spaces-versus-tabs settings. By default, VS Code may detect these from the file's existing content, so two files can be formatted with different indentation.
What the transformation actually changes
Pretty printing is not reversal, mirroring or a font or Unicode style change. No characters are replaced with look-alikes, and nothing is reordered for display. The formatter edits the stored text by inserting, removing or adjusting whitespace between tokens: spaces, tabs and line breaks. Content inside string values is left as written, and the built-in formatter keeps keys in their original order.
An illustrative example with two-space indentation:
{"name":"widget","tags":["a","b"],"size":{"w":2,"h":3}}
{
"name": "widget",
"tags": [
"a",
"b"
],
"size": {
"w": 2,
"h": 3
}
}
Both versions parse to the same data. Only whitespace outside the quoted strings differs. The example has no emoji or combining marks, but those would sit inside strings and would not be touched either. This is a constructed illustration of the mechanism, not a captured test output. Exact line-breaking choices, such as whether short arrays stay on one line, can differ between formatters and settings.
Some tools work differently: they parse the JSON into data and then write it back out. Command-line utilities and some extensions do this. Re-serializing can change more than whitespace. Depending on the tool, it may alter how numbers are written, escape non-ASCII characters, drop duplicate keys or sort keys. If exact values matter, confirm which approach your formatter uses before trusting its output.
Formatting is not validation
Successful formatting does not prove the JSON is valid. VS Code reports syntax problems separately, as squiggles in the editor and entries in the Problems panel. If a schema is associated with the file, it also checks structure against that schema. Check the Problems panel rather than relying on how the formatted output looks. Also note the difference between strict JSON and JSON with Comments. Comments and trailing commas that the editor accepts in a .jsonc file or a settings file will be rejected by many strict JSON parsers.
Will it stay readable after copying or sharing?
Pretty-printed JSON is still ordinary plain text, so pasting it into another editor, a terminal or a code block in documentation keeps both the data and the indentation. A few caveats apply:
- Rich-text destinations: by default, VS Code can copy text with syntax highlighting, so pasting into an email or word processor may bring colors and fonts along. The editor.copyWithSyntaxHighlighting setting controls this. The JSON characters themselves are unaffected.
- Whitespace-collapsing destinations: some chat tools, web forms and HTML contexts collapse or strip indentation. The data still parses, but the readability gain is lost. Wrapping the JSON in a code block, where the destination supports one, usually helps. Behavior varies by destination and has not been tested here.
- Search and editing: line-by-line tools such as grep and diff see a different layout after formatting. A value that was on one line may now span several, which can change search results and make diffs much larger.
- Assistive technology: indentation and line breaks may make nested structure easier to follow with a screen reader, because each key-value pair sits on its own line. No documentation or scoped test was supplied to confirm how specific screen readers announce indentation in VS Code, so treat this as unverified.
When compact or untouched JSON is the better choice
- Payloads sent over a network or stored at scale, where the extra whitespace adds size with no benefit to machines.
- Content covered by a hash, checksum or digital signature over the exact bytes. Reformatting will make verification fail even though the data is unchanged.
- Files under version control whose established style differs from your formatter settings. Reformatting the whole file buries real changes in whitespace noise. Format Selection, or matching the existing style, avoids this.
- Fixtures or test data compared byte for byte.
Going from pretty-printed back to compact is a separate task. The commonly used built-in commands are oriented toward formatting, and compacting JSON usually means an extension or an external tool. Check whether that tool re-serializes the data, as described above.
Evidence gaps
No product-specific evidence was supplied for this review: no version numbers, performance figures on large files, or results from destination or screen-reader testing. The behavior described here reflects VS Code's documented commands and settings and the JSON specification's treatment of whitespace. Exact output can vary with your VS Code version, installed formatter extensions and indentation settings, so check a sample before relying on it in a workflow.