Format JSON in Visual Studio Code

How VS Code's built-in JSON formatter works, what it changes and what it leaves alone, how it differs from extension formatters, and when to keep JSON compact instead.

Visual Studio Code ships with JSON language support that includes a formatter. You can pretty-print a JSON file without installing anything. This review covers what the built-in formatter changes, what it leaves alone, when an extension makes sense, and when compact or unformatted JSON is the better choice. No hands-on test results or measurements were supplied for this article. The guidance below relies on documented editor behavior and the JSON format itself, and it marks the points that need checking in your own setup.

Short verdict: is formatting JSON in VS Code suitable for your use?

For reading, reviewing and hand-editing JSON, the built-in formatter is a sound default. Its main benefit is that it changes only the layout: indentation and line breaks. Keys, values, strings and key order stay as they are, so a valid document means the same thing to any standard JSON parser before and after formatting. The decisive tradeoff is size and diff noise. Formatted JSON takes up more bytes and more lines, and reformatting a file that is under version control can produce a diff that touches nearly every line. Keep JSON compact or unformatted when it is sent over a network or embedded in a single-line context. The same applies when other tools or people expect a file's existing layout to stay untouched.

How to format a JSON file

  • Open a file that VS Code recognizes as JSON. Files ending in .json are detected automatically. For pasted or untitled content, use the Change Language Mode command and choose JSON.
  • Run Format Document from the Command Palette, or use the default shortcut: Shift+Alt+F on Windows, Shift+Option+F on macOS, Ctrl+Shift+I on Linux.
  • To format only part of a file, select it and run Format Selection.
  • To format automatically, enable the editor.formatOnSave setting. You can limit it to JSON by placing it inside a [json] language-specific settings block.

Keyboard shortcuts can be remapped, and an installed extension may claim the format command. If a shortcut does nothing or produces unexpected output, check the Keyboard Shortcuts editor and the editor.defaultFormatter setting for JSON.

What formatting actually changes

In this context, "format" means arranging whitespace. It has nothing to do with fonts, styled Unicode characters or visual effects. The JSON specification treats whitespace between tokens as insignificant. A formatter can therefore add or remove spaces, tabs and line breaks outside of strings without changing the data a parser reads. Whitespace inside string values is part of the data, and a correct formatter leaves it unchanged. Formatting changes the characters stored in the file, unlike a display-only effect. What it changes is layout, not meaning.

A short illustrative example of compact input:

{"name":"demo","tags":["a","b"],"active":true,"note":"two  spaces"}

After formatting with four-space indentation, the same data reads:

{
    "name": "demo",
    "tags": [
        "a",
        "b"
    ],
    "active": true,
    "note": "two  spaces"
}

The key order, the values and the double space inside the "note" string are unchanged. Only the whitespace between tokens differs. The exact layout is illustrative. Indentation follows your editor settings, mainly editor.tabSize and editor.insertSpaces. Indentation detection can also adopt the style already used in an open file, so the same command may indent differently in different files.

What it does not do

  • It does not sort keys. Some extensions offer sorting as a separate action. Key order carries no meaning in a JSON object under the specification, but some programs, tests and human reviewers depend on it.
  • It does not repair invalid JSON. Syntax problems such as trailing commas, missing quotes or unbalanced brackets appear as diagnostics in the editor and the Problems panel. Fix them before relying on the formatted result.
  • It does not prove the data is correct for your application. Checking syntax is different from checking that the right fields and types are present. For structure, VS Code can validate JSON against a JSON Schema when one is associated with the file.
  • It does not by itself make the file smaller. Pretty-printing adds whitespace. If you need compact output, look for a minify feature in an extension or use a separate tool, and confirm the result with a parser.

JSON versus JSON with Comments

VS Code has a separate language mode, JSON with Comments (JSONC), which it uses for its own settings files and some configuration formats. JSONC allows comments, which standard JSON parsers reject. Formatting a JSONC file keeps the comments, but the output is still not plain JSON. If a file must be read by a strict JSON parser, make sure it is in JSON mode and contains no comments. The language mode shown in the status bar tells you which rules the editor is applying.

Built-in formatter or an extension?

Need | Built-in JSON formatter | Formatter extension
Pretty-print a valid file | Yes, no install required | Yes
Consistent style shared across a team or repository | Through shared editor settings | Often through a project config file read by the extension and by command-line tooling
Sort keys, minify or convert formats | Not part of the default format command | Depends on the extension; check its documentation
One formatter across many languages | JSON-specific | Some extensions cover several languages

An extension is worth adding when you need the same formatting result outside the editor, for example in a pre-commit check or a build step, or when you need an operation the built-in formatter does not perform. If several formatters are installed, set editor.defaultFormatter for JSON so it is clear which one runs. No comparison of specific extensions was supplied for this review. Before relying on one, check its documented behavior, including whether it sorts keys or changes line endings, and any data-handling statements in its documentation. A clean marketplace listing is not evidence of how an extension treats your data.

Will the result stay readable and usable after copying or sharing?

Formatted JSON is ordinary plain text made of standard characters, spaces and line breaks. Copying it out of VS Code carries over the text itself. Whether the layout survives depends on the destination. Code blocks in documentation and issue trackers generally preserve indentation. Rich-text fields, some chat clients and email bodies may collapse spaces, rewrap lines or substitute typographic quotes. Curly quotes in particular break the JSON for parsers. These are general risks, not tested results for any named product. If the data must be parsed after sharing, paste it back into the editor and confirm it still validates, or share it as a file attachment instead.

Other practical effects to weigh:

  • Search: a text search for a pattern that spans keys and values on one line may stop matching once the formatter puts them on separate lines. A query tool that parses the JSON is not affected by layout.
  • Editing and version control: reformatting a whole file mixes layout changes with content changes in a review. Formatting in a separate commit, or agreeing on one formatter and its settings, keeps diffs readable.
  • Large files: VS Code may turn off some language features for very large files to stay responsive. No size thresholds or timings were provided for this article, so test with your own data if you work with large exports.
  • Line endings: Windows-style and Unix-style line breaks are both valid JSON whitespace, but switching between them adds noise to diffs. The editor's end-of-line setting and your version-control configuration decide which one is used.

On accessibility, indented, line-broken JSON is generally easier to follow visually than a single long line, and its reading order matches the order of the data. Formatting does not substitute or rearrange characters, so a screen reader encounters the same characters it would in the compact form. How a particular screen reader announces indentation, brackets and punctuation depends on the tool and its settings. No documentation or test covering a specific assistive technology was supplied, so treat that as unverified for your setup.

When to keep JSON compact or plain

  • Payloads sent over a network or stored in bulk, where extra whitespace adds size without benefit to a machine reader.
  • Single-line formats such as JSON Lines, where each record must stay on one line. Pretty-printing a record breaks the format.
  • Files generated and checked by other tools that expect a specific layout, where reformatting causes spurious changes.
  • Values pasted into contexts that require a single line, such as some environment variables or command-line arguments.

Gaps in the available evidence

This review draws on documented VS Code behavior and the JSON specification. No measurements, version-specific test results, extension comparisons or assistive-technology tests were supplied. Settings names and shortcuts are the documented defaults and can differ in customized installations. Before rolling a formatting workflow out to a team, confirm the behavior on a sample of your own files: check that the output parses, that key order and string contents are intact, and that the result survives the places where you share it.