AdvertisementLeaderboard · 728 × 90

How to Format JSON in VS Code (Shortcuts, Settings & Fixes)

VS Code ships with a capable JSON formatter — most people just never wire up the shortcuts and settings that make it automatic. Here is the complete setup, plus fixes for the errors that stop it working.

The keyboard shortcuts

  • Windows/Linux: Shift + Alt + F
  • Mac: Shift + Option + F
  • Or open the Command Palette (Ctrl/Cmd + Shift + P) and run Format Document.

To format only a highlighted section, select it and run Format Selection (Ctrl + K, Ctrl + F). This is invaluable when a JSON blob is embedded inside a larger file.

Format on save (set and forget)

Add to your settings.json:

{
  "editor.formatOnSave": true,
  "editor.formatOnPaste": true,
  "[json]": {
    "editor.defaultFormatter": "vscode.json-language-features"
  },
  "[jsonc]": {
    "editor.defaultFormatter": "vscode.json-language-features"
  }
}

With formatOnPaste enabled, minified JSON pastes in already pretty-printed — the single biggest quality-of-life setting in this list.

Controlling indentation

The JSON formatter follows the editor's tab settings: editor.tabSize and editor.insertSpaces. Set them per-language inside the [json] block if you want JSON at 2 spaces while other files use 4. VS Code may also auto-detect indentation from the file (editor.detectIndentation) and override your default — disable that if files keep coming out at the "wrong" width.

Fixing the common failures

"There is no formatter for 'json' files installed"

Usually the file's language mode is wrong (plain text) or a broken extension has claimed JSON. Click the language indicator in the status bar and set it to JSON, or reset the default formatter as in the settings above.

Formatter does nothing at all

The document is syntactically invalid — VS Code will not format broken JSON. Look for red squiggles, or paste the content into our JSON validator to get the exact error line, fix it, and format again.

Comments flagged as errors

Files like settings.json and tsconfig.json are JSONC — JSON with comments. If your own .json file legitimately contains comments, switch its language mode to "JSON with Comments", or better, keep pure data files comment-free so every consumer can parse them.

Sorting keys

Built-in formatting preserves key order. For alphabetised keys — useful for diff-friendly config — install a small extension such as "Sort JSON objects", or run the file through python -m json.tool --sort-keys as covered in our Python guide.

When the browser is faster

For a payload sitting in your clipboard — an API response, a log line, something from Slack — opening an editor tab is overhead. Paste it into our free JSON formatter instead: instant pretty-print, validation with line numbers, minify, and nothing ever uploaded.

AdvertisementIn-content · responsive
→ Outside the editor? Format JSON in your browser in one clickFree · 100% client-side · no signup
AdvertisementAnchor · 728×90 / 320×50